day05 基本项目
1、求其最大公约数和最小公倍数
import java. util. Scanner;
class ForTest {
public static void main ( String[ ] args) {
Scanner scan = new Scanner ( System. in) ;
System. out. println ( "请输入第一个正整数:" ) ;
int m = scan. nextInt ( ) ;
System. out. println ( "请输入第二个正整数:" ) ;
int n = scan. nextInt ( ) ;
int min = ( m <= n) ? m : n;
for ( int i = min; i >= 1 ; i-- ) {
if ( m % i == 0 && n % i == 0 ) {
System. out. println ( "最大公约数为:" + i) ;
break ;
}
}
int max = ( m >= n) ? m : n;
for ( int i = max; i <= m * n; i++ ) {
if ( i % m == 0 && i % n == 0 ) {
System. out. println ( "最小公倍数:" + i) ;
break ;
}
}
}
}
2、嵌套循环
class ForForTest {
public static void main ( String[ ] args) {
for ( int i = 1 ; i <= 6 ; i++ ) {
System. out. print ( '*' ) ;
}
System. out. println ( "\n" ) ;
/ *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* /
for ( int j = 1 ; j <= 4 ; j++ ) {
for ( int i = 1 ; i <= 6 ; i++ ) {
System. out. print ( '*' ) ;
}
System. out. println ( ) ;
}
for ( int i = 1 ; i <= 5 ; i++ ) {
for ( int j = 1 ; j <= i; j++ ) {
System. out. print ( "*" ) ;
}
System. out. println ( ) ;
}
for ( int i = 1 ; i <= 4 ; i++ ) {
for ( int j = 1 ; j <= 5 - i; j++ ) {
System. out. print ( "*" ) ;
}
System. out. println ( ) ;
}
}
}
3、打印菱形
class ForForTest
{
public static void main ( String[ ] args)
{
for ( int i = 1 ; i <= 5 ; i ++ )
{
for ( int j = 1 ; j <= ( 5 - i) ; j ++ )
{
System. out. print ( ' ' ) ;
}
for ( int k = 1 ; k <= i; k ++ )
{
System. out. print ( '*' ) ;
System. out. print ( ' ' ) ;
}
System. out. println ( ) ;
}
for ( int i = 1 ; i <= 4 ; i ++ )
{
for ( int k = 1 ; k <= i ; k++ )
{
System. out. print ( ' ' ) ;
}
for ( int j = 1 ; j <= ( 5 - i) ; j++ )
{
System. out. print ( '*' ) ;
System. out. print ( ' ' ) ;
}
System. out. println ( ) ;
}
}
}
4、九九乘法表
class MultiplicationTable
{
public static void main ( String[ ] args)
{
for ( int i = 1 ; i <= 9 ; i++ )
{
for ( int j = 1 ; j <= i ; j++ )
{
System. out. print ( i + "*" + j + "=" + i* j) ;
System. out. print ( " " ) ;
}
System. out. println ( ) ;
}
}
}
5、100以内所有质数(未优化)
class PrimeNumberTest {
public static void main ( String[ ] args) {
boolean isFlag = true ;
for ( int i = 2 ; i <= 100 ; i++ ) {
for ( int j = 2 ; j < i; j++ ) {
if ( i % j == 0 ) {
isFlag = false ;
}
}
if ( isFlag == true ) {
System. out. println ( i) ;
}
isFlag = true ;
}
}
}
6、100以内所以质数(优化1)
class PrimeNumberTest1 {
public static void main ( String[ ] args) {
boolean isFlag = true ;
int count = 0 ;
long start = System. currentTimeMillis ( ) ;
for ( int i = 2 ; i <= 100000 ; i++ ) {
for ( int j = 2 ; j <= Math. sqrt ( i) ; j++ ) {
if ( i % j == 0 ) {
isFlag = false ;
break ;
}
}
if ( isFlag == true ) {
count++ ;
}
isFlag = true ;
}
long end = System. currentTimeMillis ( ) ;
System. out. println ( "质数的个数为:" + count) ;
System. out. println ( "所花费的时间为:" + ( end - start) ) ;
}
}
7、100以内所以质数(优化3)
class PrimeNumberTest2 {
public static void main ( String[ ] args) {
int count = 0 ;
long start = System. currentTimeMillis ( ) ;
label: for ( int i = 2 ; i <= 100000 ; i++ ) {
for ( int j = 2 ; j <= Math. sqrt ( i) ; j++ ) {
if ( i % j == 0 ) {
continue label;
}
}
count++ ;
}
long end = System. currentTimeMillis ( ) ;
System. out. println ( "质数的个数为:" + count) ;
System. out. println ( "所花费的时间为:" + ( end - start) ) ;
}
}