Java由浅入深小程序
1、求三个和尚的最高身高Scanner
import java. util. Scanner ;
public class Demo01 {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请输入第一个和尚的身高" ) ;
int height1 = sc. nextInt ( ) ;
System . out. println ( "请输入第二个和尚的身高" ) ;
int height2 = sc. nextInt ( ) ;
System . out. println ( "请输入第三个和尚的身高" ) ;
int height3 = sc. nextInt ( ) ;
int meHeight = height1> height2 ? height1: height2;
int maxHeight = meHeight> height3? meHeight: height3;
System . out. println ( "最高的和尚身高是:" + maxHeight+ "cm" ) ;
}
}
2、判断奇数还是偶数if…else
import java. util. Scanner ;
public class Demo02 {
public static void main ( String [ ] inge) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请输入一个整数" ) ;
int i = sc. nextInt ( ) ;
if ( ( i% 2 ) == 0 ) {
System . out. println ( i+ "是偶数" ) ;
} else {
System . out. println ( i+ "是奇数" ) ;
}
System . out. println ( "请输入整数" ) ;
}
}
3、考试奖励if…else if
import java. util. Scanner ;
public class Demo03 {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "总分100分,请输入分数:" ) ;
int score = sc. nextInt ( ) ;
if ( score > 100 || score < 0 ) {
System . out. println ( "您输入的分数有误!" ) ;
} else if ( score >= 60 && score < 70 ) {
System . out. println ( "小明考试" + score+ "分,不会被打" ) ;
} else if ( score < 60 ) {
System . out. println ( "小明考试" + score+ "分,考试不及格!回家要被打" ) ;
} else if ( score >= 70 && score < 80 ) {
System . out. println ( "小明考试" + score+ "分,获得100块零花钱" ) ;
} else if ( score >= 80 && score < 90 ) {
System . out. println ( "小明考试" + score+ "分,获得获得200块零花钱" ) ;
} else if ( score >= 90 && score <= 100 ) {
System . out. println ( "小明考试" + score+ "分,获得获得300块零花钱" ) ;
}
}
}
4、输出一个数判断星期几 switch
import java. util. Scanner ;
public class Demo04 {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请输入数字【1-7】" ) ;
int week = sc. nextInt ( ) ;
switch ( week) {
case 1 :
System . out. println ( "星期一" ) ;
break ;
case 2 :
System . out. println ( "星期二" ) ;
break ;
case 3 :
System . out. println ( "星期三" ) ;
break ;
case 4 :
System . out. println ( "星期四" ) ;
break ;
case 5 :
System . out. println ( "星期五" ) ;
break ;
case 6 :
System . out. println ( "星期六" ) ;
break ;
case 7 :
System . out. println ( "星期天" ) ;
break ;
default :
System . out. println ( "输入错误" ) ;
break ;
}
System . out. println ( "结束!" ) ;
}
}
5、判断季节 switch
import java. util. Scanner ;
public class Demo05 {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请输入月份" ) ;
int month = sc. nextInt ( ) ;
switch ( month) {
case 12 :
case 1 :
case 2 :
System . out. println ( "现在是" + month + "月,属于冬季" ) ;
break ;
case 3 :
case 4 :
case 5 :
System . out. println ( "现在是" + month + "月,属于春季" ) ;
break ;
case 6 :
case 7 :
case 8 :
System . out. println ( "现在是" + month + "月,属于夏季" ) ;
break ;
case 9 :
case 10 :
case 11 :
System . out. println ( "现在是" + month + "月,属于秋季" ) ;
break ;
default :
System . out. println ( "输入有误" ) ;
}
}
}
6、输出1-5和5-1的数字for
public class Demo06 {
public static void main ( String [ ] args) {
for ( int i = 1 ; i <= 5 ; i++ ) {
System . out. println ( i) ;