1. switch-case语句例题:
从键盘分别输入年、月、日,判断这一天时当年的第几天
while (true){
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = sc.nextInt();
System.out.println("请输入月份:");
int month = sc.nextInt();
System.out.println("请输入日期:");
int day = sc.nextInt();
if(year == 0){System.out.println("退出!");break;}
//判断是否闰年
boolean flag = false;
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)){
flag = true;
}
int sumDays = day;
switch(month){
case 12:
sumDays += 30;
case 11:
sumDays += 31;
case 10:
sumDays += 30;
case 9:
sumDays += 31;
case 8:
sumDays += 31;
case 7:
sumDays += 30;
case 6:
sumDays += 31;
case 5:
sumDays += 30;
case 4:
sumDays += 31;
case 3:
if(flag){
sumDays += 29;
}else{
sumDays += 28;
}
case 2:
sumDays += 31;
case 1:
break;
}
System.out.println("这是2019年的第" + sumDays + "天");
}
2. switch-case语句例题
从键盘上读入一个学生成绩,输出其对应的等级 大等于90为A [70, 90)为B, [60, 70)为C,小于60为D
while(true){
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生成绩:");
int score = sc.nextInt();
if(score < 0){
System.out.println("退出!");
break;
}
switch(score/10){
case 10:
case 9:
System.out.println("A");
break;
case 8:
case 7:
System.out.println("B");
break;
case 6:
System.out.println("C");
break;
default:
System.out.println("D");
}
}
3. switch后面使用的表达式可以是哪些数据类型的
byte short char int 枚举 String
4. for循环例题:求两个正整数的最大公约数(gcd)和最小公倍数(lcm)
while(true){
Scanner sc = new Scanner(System.in);
System.out.println("请输入两个正整数:");
int m = sc.nextInt();
int n = sc.nextInt();
if(m < 0){
System.out.println("退出!");
break;
}
int gcd = 0;
int lcm = 0;
int min = (m < n) ? m : n;
int max = (m > n) ? m : n;
//求最大公约数
for(int i = min; i >= 1; i --){
if(m % i == 0 && n % i == 0){
gcd = i;
break;
}
}
//求最小公倍数
int j ;
for(int i = 1;i <= min ;i ++ ){
j = i * max;
if(j % min == 0){
lcm = j;
break;
}
}
System.out.println(m + "和" + n + "的最大公约数:" + gcd + ",最小公倍数:" + lcm);
}
5. 循环综合练习题:输出质数
class PrimeNumberTest1 {
public static void main(String[] args) {
//输出100以内的质数
//方式一:
/*
boolean flag = true;//默认是质数
int count = 0;
long start = System.currentTimeMillis();
for(int i = 2; i <= 100000; i ++){
//for(int j = 2; j < i; j ++){
//for(int j = 2; j <= i / 2; j ++){//优化2:针对质数有效 (如果不加等号会将4判断为质数)
for(int j = 2; j <= Math.sqrt(i); j ++){//优化3
if(i % j == 0){//不是质数
flag = false;
break;//优化1:针对合数有效,找到一个非1和本身的因数就不再循环
}
}
if(flag){
count ++;
//System.out.print(i + " ");
}
flag = true;
}
long end = System.currentTimeMillis();
System.out.println("质数个数为:" + count);
System.out.println("用时:" + (end - start));
*/
//方式二:
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));
}
}
//不输出质数,只输出个数的情况下(100000以内质数)
//方式一:
//未优化: 用时20596
//优化1:用时2024
//优化2:用时1650
//优化3:用时20
//方式二:用时20
6. 带标签的break和continue的使用
label : for(int i = 2; i <= 100000; i ++){
for(int j = 2; j <= Math.sqrt(i); j ++){
if(i % j == 0){//不是质数
continue label;
}
}
count ++;
}