目标是这样的:
第 1 行:A-L
第 2 行:M-Z
编写一个程序,将学生的全名(名字和姓氏)作为输入,并打印出学生应该所在的行。名字或姓氏将包含任何空格。输入中只有一个空格,位于名字和姓氏之间。
我不知道如何让它读取字符 A - L 和 M- Z。
import java.util.Scanner;
public class SeatingChart {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char row1 = 'A' ,'L';
char row2 = 'M' ,'Z';
System.out.println(" Enter the student's last name: ");
String name = in.next();
char initial = name.charAt(0);
if (initial = row1) {
System.out.println(" This student can sit anywhere in row 1. ");
}
if (initial = row2) {
System.out.println(" This student can sit anywhere in row 2. ");
}
in.close();
}
}
这是我到目前为止所拥有的,但是代码对于声明字符 A - L 和字符 M - Z 是不正确的。我如何解决这个问题以使其读取这些字符列表?
像这样的东西应该有效(未经测试):
if((initial >= 'A' && initial <= 'L') || (initial >= 'a' && initial <= 'l')){
// If letter is between 'A' and 'L' or 'a' and 'l'
System.out.println(" This student can sit anywhere in row 1. ");
} else if((initial >= 'M' && initial <= 'Z') || (initial >= 'm' && initial <= 'z')){
// If letter is between 'M' and 'Z' or 'm' and 'z'
System.out.println(" This student can sit anywhere in row 2. ");
}
如果输入是full名称添加此:
try{
char initial = name.split(" ")[1].charAt(0);
} catch(Exception e){
System.out.println("Invalid input!");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)