我正在编写一个程序来打印出学生的姓名和成绩。如果我只输入他们的名字就行了,当我进入他们的姓氏时我得到一个错误。我想知道我是否忽略了String名称。
package project_2;
import java.util.Scanner;
public class Project_2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input.next();
System.out.print("Enter Grade: \t");
double Grade = input.nextDouble();
if(Grade>=88)
System.out.println(name + "You have an A!");
else if (Grade<=87 && Grade>=80)
System.out.println(name + "You have a B!");
else if (Grade<=79 && Grade>=67)
System.out.println(name + "You have a C!");
else if (Grade<=66 && Grade>=60)
System.out.println(name + "You have a D!");
else
System.out.println(name + "You have a F!");
}
}
答案 0 :(得分:0)
这样做:
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input.nextLine();
System.out.print("Enter Grade: \t");
然后你可以使用全名。
输出:
Enter your name: Jack Davidson
Enter Grade: 88
请注意,使用next()
只会返回空格前的内容。返回当前行后,nextLine()
会自动将扫描仪向下移动。