问题描述
目标是这样的:
编写一个程序,该程序将学生的全名(姓)作为输入,并打印出学生应位于的行。名字或姓氏将包含任何空格。输入中只有一个空格,该空格将位于名字和姓氏之间。
Write a program that takes as input a student's full name (first last) and prints out the row the student should be in. The first nor last name will contain any spaces. There will be only one space in the input and that will be between the first and last name.
我不确定如何制作
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是不正确的。如何解决这个问题,才能使其读取这些字符列表?
This is what I have so far, but The code is incorrect for declaring characters A - L and characters M - Z. How would I fix this to make it read these list of characters?
推荐答案
类似的东西应该可以工作(未经测试):
Something like this should work (not tested):
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 ,则添加名称
And if the input would be full name add this:
try{
char initial = name.split(" ")[1].charAt(0);
} catch(Exception e){
System.out.println("Invalid input!");
}
这篇关于确定char值是否在char范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!