import java.util.Scanner;
public class JavaMovies {
Scanner det = new Scanner(System.in);
int age = det.nextInt();
public static void main(String[] args){
calculator();
}
public static void calculator(){
Scanner det = new Scanner(System.in);
String rMovie = "Attack of the nerds";
String mMovie = "Nerd lyfe";
String pgMovie = "Nerd adventures";
String gMovie = "Nerd playtime";
System.out.println("These are the movies that are playing this weekend"
+ rMovie
+ mMovie
+ pgMovie
+ gMovie);
System.out.println("Please input your age");
int age = det.nextInt();
System.out.print("Input the movie you wish to see:");
String x = det.nextLine(); //Program ends here...
if(x.equals("Attack of the nerds") & age > 17){
System.out.println("You can see Attack of the nerds");
}else if(x.equals("Attack of the nerds") & age < 18){
System.out.println("You can see attack of the nerds!");
}if(x.equals("Nerd lyfe")& age > 15){
System.out.println("You can see Nerd lyfe");
}else if(x.equals("Nerd lyfe")& age < 16){
System.out.println("You can not see Nerd lyfe");
}if (x.equals("Nerd adventures")& age > 10){
System.out.println("You can see Nerd adventures");
}else if(x.equals("Nerd adventures")& age < 11){
System.out.println("You can not see Nerd adventures");
}if(x.equals("Nerd playtime")& age > 5){
System.out.println("You can see Nerd playtime");
}else if(x.equals("Nerd playtime")& age < 6){
System.out.println("You can not see Nerd playtime");
}
}
}
在if语句的开头,一切都没问题,我该怎么做才能解决这个问题?
控制台输出(使用netbeans) 跑: 这些是本周末正在播放的电影攻击者书呆子游戏Nerd冒险游戏时间 请输入您的年龄 14 输入您想看的电影:BUILD SUCCESSFUL(总时间:4秒)
答案 0 :(得分:1)
我认为问题在于nextInt
并没有用完整条线。如果您在提示年龄时键入32
,nextInt
将使用3
和2
字符,但仍有换行符字符在输入字符串中。然后,当您拨打nextLine
时,效果是返回当前行中剩余的所有内容,直到下一个换行符。由于当前行仍有换行符,因此会String x = det.nextLine()
将x
设置为空字符串。
在提示观看电影之前尝试det.nextLine();
。 (您不需要将其分配给变量;只需抛弃结果。)这将导致扫描程序使用换行符,以便下一个nextLine()
将获得另一行输入。