gpt4 book ai didi

java - 努力在我的代码中使用大写和小写

转载 作者:行者123 更新时间:2023-12-02 09:46:55 24 4
gpt4 key购买 nike

我找不到一种方法让代码同时使用大写和小写字母,我只能找到一种方法让它运行一个或另一个......已经尝试了一段时间但仍然没有管理,你看这很新!提前致谢!

import java.util.*;
public class LectureTimes {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System. in );
char letter;
System.out.println("Enter your class and" + " you will recieve your lecture time");

do {
letter = sc.next().charAt(0);
} while (letter < 'a' || letter > 'd');

if (letter == 'A') {
System.out.println("Your class is at 10:00");
} else {
if (letter == 'a') {
System.out.println("Your class is at 10:00");
} else {
if (letter == 'B') {
System.out.println("Your class is at 12:00");
} else {
if (letter == 'b') {
System.out.println("Your class is at 12:00");
} else {
}
if (letter == 'C') {
System.out.println("Your class is at 14:00");
} else {
if (letter == 'c') {
System.out.println("Your class is at 14:00");
} else {
System.out.println("Your group does not exist, please select one that does...");
}
}
}
}
}
}
}

最佳答案

由于还没有人发布 switch case 解决方案,这里是:

switch (letter) {
case 'A':
case 'a':
System.out.println("Your class is at 10:00");
break;
case 'B':
case 'b':
System.out.println("Your class is at 12:00");
break;
case 'C':
case 'c':
System.out.println("Your class is at 14:00");
break;
default:
System.out.println("Your group does not exist, please select one that does...");
break;
}

就我个人而言,我发现这是最具可读性的解决方案。

关于java - 努力在我的代码中使用大写和小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34441462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com