java请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母.代码
时间: 2023-10-21 20:13:29 浏览: 83
Java基础编程:输入星期几的第一个字母来判断一下是星期几
答案如下:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String day = scanner.nextLine();
char firstLetter = Character.toLowerCase(day.charAt(0));
char secondLetter = Character.toLowerCase(day.charAt(1));
String daysOfWeek = "uomtwrf";
int index = daysOfWeek.indexOf(firstLetter);
if (index < 0) {
System.out.println("Invalid input!");
return;
}
String[] dayNames = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
String result = dayNames[index];
if (firstLetter == 't') {
result = secondLetter == 'u' ? "Tuesday" : "Thursday";
} else if (firstLetter == 's') {
result = secondLetter == 'a' ? "Saturday" : "Sunday";
}
System.out.println("The day of week is " + result);
}
}
```
该代码可以读取用户输入的字符串,判断第一个字母是否是星期几的首字母,并根据情况继续判断第二个字母。如果输入无效,则输出错误信息。最终输出判断结果,即星期几的完整名称。
阅读全文