import javax.swing.
JOptionPane;public class Lab9_1 {
public static void main(String[] args) {
String[] number = new String[5];
int[] num = new int[5];
int tempmax;for(int i=0; i<.number.length; i++){
number[i] =JOptionPane.showInputDialog("Enter number "+i+" : ");
num[i] = Integer.parseInt(number[i]);}
tempmax = num[0];
for(int i=1; i<.num.length; i++)
if (tempmax
tempmax = num[i];
System.out.println("The max value is: " + tempmax);
System.exit(0);}}
.........................................................
Study the following program which will randomly generate a value between 1 to 20:
public class testRandom {
public static void main(String[] args) {
int x = 1 + (int) (Math.random()*20);
System.out.println(x);}}
Write a program that creates two 5×4 matrixes containing random integers in the range 1-20. Then, the program adds the corresponding elements of the first two matrixes to athird 5×4 matrix.
Matrix 1 is :14 5 20 91
4 5 12 19
14 3 2 4
19 5 17 12
3 3 4 13
Matrix 2 is :1 4 7 8
18 10 3 15
14 9 15 4
1 12 4 4
16 1 6 11
Matrix 3 (= Matrix1 + Matrix 2) is :
15 9 27 17
32 15 15 34
28 12 17 8
20 17 21 16
19 4 10 24
solution:
public class Lab9_4 {
public static void main(String[] args) {
int[][] matrix1 = new int[5][4],
matrix2 = new int[5][4], matrix3 = newint[5][4];
String output="";
for (int row = 0; row <. 5; row++)
for (int column = 0; column <. 4; column++) {
matrix1[row][column] = 1 + (int) (Math.random() * 20);
matrix2[row][column] = 1 + (int) (Math.random() * 20);
matrix3[row][column] = matrix1[row][column]+ matrix2[row][column];}
output += "\nMatrix 1 is :\n";
for (int row = 0; row <. matrix1.length; row++) {
for (int column = 0; column <. matrix1[row].length; column++) {
output += matrix1[row][column] + "\t";}
output += "\n";}
output += "\nMatrix 2 is :\n";
for (int row = 0; row <. matrix2.length; row++) {
for (int column = 0; column <. matrix2[row].length; column++) {
output += matrix2[row][column] + "\t";}
output += "\n";}
output += "\nMatrix 3 (= Matrix1 + Matrix 2) is :\n";
for (int row = 0; row <. matrix3.length; row++) {
for (int column = 0; column <. matrix3[row].length; column++) {
output += matrix3[row][column] + "\t";}
output += "\n";}
System.out.println(output);}}
沒有留言:
張貼留言