JAVA basic instance (I)
1. Write a method and print the 9-9 multiplication table using a for loop /**
* Print a 9-9 multiplication table in a for Loop
*/
Public void nineNineMultiTable ()
{
For (int I = 1, j = 1; j <= 9; I ++ ){
System. out. print (I + "*" + j + "=" + I * j + "");
If (I = j)
{
I = 0;
J ++;
System. out. println ();
}
}
}
2. Write a method to determine whether any integer has a prime number (the prime number can be obtained except 1 multiplied by its own number )/**
* Determine whether any integer is a prime number.
* @ Paramn
* @ Returnboolean
*/
Public boolean isprimes (int n)
{
For (int I = 2; I <= Math. sqrt (n); I ++ ){
If (n % I = 0)
{
Return false;
}
}
Return true;
}
3. Write a method, input any integer, and return its factorial (for example, 3 is 1*2*3 = 6 )/**
* Obtain the factorial of any integer
* @ Paramn
* @ Returnn!
*/
Publicint factorial (int n)
{
// Recursion
If (n = 1)
{
Return 1;
}
Return n * factorial (n-1 );
// Non-recursion
// Int multi = 1;
// For (int I = 2; I <= n; I ++ ){
// Multi * = I;
//}
// Return multi;
}