JAVA basic instance (I)

Source: Internet
Author: User

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;
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.