Java 基础代码练习题
1.一直青蛙在一口11米深的井底向上爬,白天向上爬3米,晚上向下滑2米,总共需要几天可以爬出.
package com. Work;
public class Frog_Dome {
public static void main ( String[ ] args) {
int height = 11 ;
int i= 1 ;
outer: while ( height>= 0 ) {
height-= 3 ;
if ( height<= 0 ) {
break outer;
}
height+= 2 ;
i++ ;
}
System. out. println ( "青蛙第" + i+ "天爬出了井" ) ;
}
}
2.猴子吃桃问题。猴子第一天摘下若干个桃子,当时就吃了一半,还不过瘾,就又吃了一个。第二天又将剩下的桃子吃掉一半,又多吃了一个。以后每天都吃前一天剩下的一半零一个。到第 10 天在想吃的时候就剩一个桃子了,求第一天共摘下来多少个桃子?
package com. Work;
public class Monkey_Dome {
public static void main ( String[ ] args) {
int i= 10 ;
int peach = 1 ;
while ( i> 0 ) {
peach = 2 * ( peach+ 1 ) ;
i-- ;
}
System. out. println ( "猴子第一天摘了" + peach+ "个桃子" ) ;
}
}
package com. Work;
public class Odd_Dome {
public static void main ( String[ ] args) {
int n= 0 ;
for ( int i = 100 ; i > 0 ; i-- ) {
if ( i % 2 == 1 ) {
System. out. println ( i) ;
n++ ;
if ( n== 5 ) {
System. exit ( 0 ) ;
}
}
}
}
}
4.水仙花数是指一个 3 位数,它的每个位上的数字的3次幂之和等于它本身。 (例如:1^3 + 5^3 + 3^3 = 153)。编程求出1-1000以内的水仙花数。
package com. Work;
public class Flawer_Dome {
public static void main ( String[ ] args) {
for ( int sum= 100 ; sum<= 999 ; sum++ ) {
int i= sum/ 100 ;
int j= ( sum/ 10 ) % 10 ;
int k= sum% 10 ;
if ( i* i* i+ j* j* j+ k* k* k== sum) {
System. out. println ( sum) ;
}
}
}
}
5.输出1–100之间的质数(大于1,只能被1和本身整除的,再没有其他因数的数)
package com. Work;
public class Num_Dome {
public static boolean func ( int num)
{
for ( int i= 2 ; i<= Math. sqrt ( 1.0 * num) ; i++ )
{
if ( num% i== 0 ) return false ;
}
return true ;
}
public static void main ( String[ ] args) {
for ( int i= 2 ; i<= 100 ; i++ )
{
if ( func ( i) == true ) System. out. println ( i) ;
}
}
}
6.输入某年某月某日,判断这一天是这一年的第几天? 年份主要是用来区分是否为闰年
package com. Work;
import java. util. Scanner;
public class New_year_Dome {
public static void main ( String[ ] args) {
int a[ ] = { 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ;
int days= 0 ;
int i;
Scanner scanner = new Scanner ( System. in) ;
System. out. println ( "请输入年份:" ) ;
int year = scanner. nextInt ( ) ;
System. out. println ( "请输入月份:" ) ;
int month = scanner. nextInt ( ) ;
System. out. println ( "请输入日号:" ) ;
int day = scanner. nextInt ( ) ;
System. out. println ( "请输入年份:" ) ;
if ( ( year% 4 == 0 && year% 100 != 0 ) || year% 400 == 0 ) {
for ( i = 1 ; i< month; i++ ) {
days+= a[ i] ;
}
System. out. println ( year+ "年为闰年,今天为" + year+ "年的第" + ( days+ day) + "天" ) ;
} else {
int j;
for ( j = 1 ; j< month; j++ ) {
if ( j== 2 ) {
days+= 28 ;
continue ;
}
days+= a[ j] ;
}
System. out. println ( year+ "年为平年,今天为" + year+ "年的第" + ( days+ day) + "天" ) ;
}
}
}
7.古典问题:有一对兔子, 从出生后第3 个月起每个月都生一对兔子, 小兔 子长到第三个月后每个月又生一对兔子, 假如兔子都不死, 问每个月的兔子总数 为多少? 假定12月?
package com. Work;
public class Rib_Dome {
public static void main ( String[ ] args) {
int a= 1 , b= 0 , sum, month;
for ( month= 1 ; month<= 12 ; month++ ) {
sum= a;
System. out. println ( "第" + month+ "月有" + ( 2 * sum) + "只兔子" ) ;
a= a+ b;
b= sum;
}
}
}
8.用户输入两个数,求两个数的最大公约数,最小公倍数
package com. Work;
import java. util. Scanner;
public class Tow_num_Dome {
public static void main ( String[ ] args) {
Scanner sc = new Scanner ( System. in) ;
System. out. println ( "请输入一个数:" ) ;
int min = sc. nextInt ( ) ;
System. out. println ( "请再输入一个数:" ) ;
int max = sc. nextInt ( ) ;
int a = min;
int b = max;
if ( max < min) {
int temp = min;
min = max;
max = temp;
} else {
while ( max % min != 0 ) {
int k = max % min;
max = min;
min = k;
}
}
System. out. println ( "最大公因数为:" + min) ;
System. out. println ( "最小公倍数为:" + a* b/ min) ;
}
}