Java Bài tập Java Bài tậpJava Java Bài tập Java Bài tậpJava Java Bài tập Java Bài tậpJava Java Bài tập Java Bài tậpJava Java Bài tập Java Bài tậpJava Java Bài tập Java Bài tậpJava Java Bài tập Java Bài tậpJava Java Bài tập Java Bài tậpJava Java Bài tập Java Bài tậpJava
Trang 1Note : Use Math.random() to create random number
Example: Create and print to screen a nxn matrix (with n>=3)
Hint:
public class Ex{
public static void main(String [] arg){
int n=3;
int a[][]=new int[n][n];
//Create random numbers from 0 to 99 for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j] = (int)(Math.random()*100);
//Output for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
System.out.printf("%3d",a[i][j]);
System.out.println();
} }
}
Trang 21 Give a 5x5 matrix (or nxn, with n>=3), please:
a) Print a series of numbers is the largest element on each row of the matrix b) Determine the smallest value and the largest based on the results of the
preceding question
2 Give a 5x5 matrix (or nxn, with n>=3), please:
a) Print all odd numbers in the matrix and average of those numbers
b) Print all even numbers in the matrix and average of those numbers
3 Give a 5x5 matrix (or nxn, with n>=3), please:
a) Print a series of numbers is the primes in the matrix and average of those numbers
b) Find the minimum prime (min) and maximum (max) is based on range
finding, printing out primes lying in the interval [min, max]
4 Give a 5x5 matrix (or nxn, with n>=3), please:
a) Print a series of numbers is the square numbers in the matrix and average
of those numbers
b) Find the minimum square number (min) and maximum (max) is based on range finding, printing out square numbers lying in the interval [min, max]
5 Give a 5x5 matrix (or nxn, with n>=3), please:
a) Find the smallest number (min) and maximum (max) on the main diagonal
of the matrix
Trang 3b) Based on the results of min and max, print out all the primes in the interval [min, max] is not in the matrix
6 Give a 5x5 matrix (or nxn, with n>=3), please:
a) Find the smallest number (min) and maximum (max) on the secondary
diagonal of the matrix
b) Based on the results of min and max, print out all the even square numbers
in the interval [min, max]
7 Give a 5x5 matrix (or nxn, with n>=3), please:
a) Print all the odd integers in the matrix
b) Determine the smallest and largest numbers based on a sequence of
question a
c) Based on the minimum (min) and maximum (max) in question b, print out the odd integers in the interval [min, max] is not in sequence of question a
8 Give a 5x5 matrix (or nxn, with n>=3), please:
a) Find the smallest prime number (min) and maximum (max) in the matrix b) Based on the above results, print out all the prime numbers in the interval [min, max]
9 Give a 5x5 matrix (or nxn, with n>=3), please:
a Calculate sum the smallest integers on each line of the matrix
b Calculate sum the largest integers on each line of the matrix