Java里面的while循环输出对应的星期问题?代码如下;

2024-11-26 01:40:02
推荐回答(5个)
回答1:

import java.util.*;
public class Test {
public static void main(String[] args) {
// TODO 自动生成方法存根
int num;
String day = "";
Scanner input = new Scanner(System.in);

do{
System.out.print("请输入数字1-3(输入0结束):");
num = input.nextInt();
switch (num) {
case 1:
day = "星期一";
System.out.println("今天是:" + day);
continue;
case 2:
day = "星期二";
System.out.println("今天是:" + day);
continue;
case 3:
day = "星期三";
System.out.println("今天是:" + day);
continue;
case 0:
break;
default:
System.out.print("请重新输入:");
num = input.nextInt();
continue;
}
}while(num != 0);
System.out.println("程序结束");
}
}

回答2:

public static void main(String[] args) {
// TODO 自动生成方法存根
int num;
String day = "";
Scanner input = new Scanner(System.in);

do{
System.out.print("请输入数字1-3(输入0结束):");
num = input.nextInt();
switch (num) {
case 1:
day = "星期一";
System.out.println("今天是:" + day);
break;
case 2:
day = "星期二";
System.out.println("今天是:" + day);
break;
case 3:
day = "星期三";
System.out.println("今天是:" + day);
break;

default:
System.out.print("请重新输入:");
num = input.nextInt();
break;
}

}while(num != 0);

System.out.println("程序结束");
}
问题在于你在while外面赋值之后,没在循环里面赋值,所以一直是一个数反复循环,造成死循环

回答3:

public static void main(String[] args) {
int num;
String day = "";
Scanner input = new Scanner(System.in);

do{
System.out.print("请输入数字1-3(输入0结束):");
num = input.nextInt();

switch (num) {
case 1:
day = "星期一";
System.out.println("今天是:" + day);
break;
case 2:
day = "星期二";
System.out.println("今天是:" + day);
break;
case 3:
day = "星期三";
System.out.println("今天是:" + day);
break;
case 0:
break;
default:
System.out.print("请重新输入:");
num = input.nextInt();
break;
}

}while(num != 0);

System.out.println("程序结束");

}

回答4:

你不是要显示英文缩写吗?我也没见缩写在哪里,就看见中文了.....

你不是要1-7吗?怎么又写成1-3了?.....

你先用一个数组保存String [] weekNames={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};

不用switch-case语句

while(true){
if(num>=1&&num<=7)
System.out.println(weekNames[num-1]);
else if(num>7||num<0)
System.out.println("重新输入..");
else //num==0
break;
}

回答5:

case X:
day = "XXX";
System.out.println("今天是:" + day);
continue;
continue改为break~~