这是代码:
import java.util.*;
public class dayName {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter the number to find out what day of the week it represents:");
int number = in.nextInt();
weekNumber(number);
}
public static String weekNumber(int number)
{
String dayNumber;
switch (number)
{
case 1: dayNumber = "Monday"; break;
case 2: dayNumber = "Tuesday"; break;
case 3: dayNumber = "Wednesday"; break;
case 4: dayNumber = "Thursday"; break;
case 5: dayNumber = "Friday"; break;
case 6: dayNumber = "Saturday"; break;
case 7: dayNumber = "Sunday"; break;
default: dayNumber = ""; break;
}
return dayNumber;
}
}
我的问题是,它可以正常编译,但是当您要求输入数字时,它不返回任何内容,而是应根据输入的数字返回日期字符串。
请记住,我正在学习Java。
最佳答案
weekNumber()
仅返回该值,但您不对其执行任何操作。
将该行更改为:
System.out.println(weekNumber(number));
关于java - 使用switch语句的方法问题,返回不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7972603/