我在“开始”时遇到错误。在底部的(开始+结束)

时间:2015-04-26 00:51:51

标签: java

我在' start'上收到错误在底部的(开始+结束)。修复它的唯一方法是添加" int start = inputFn.nextInt();"下面'扫描仪输入Fn =新扫描仪(System.in);'但这一切搞砸了。之前我能够解决它,但我忘了我是怎么做到的。请帮忙。

public static void main(String eth[]){

System.out.println("Please Enter An Integer:");
Scanner inputFn = new Scanner(System.in);


if (inputFn.hasNextInt()){
}else{
int start = inputFn.nextInt();
System.out.print("Play By The Rules And Enter An Integer");

}

System.out.println("Please Enter An Integer:");
Scanner inputSn = new Scanner(System.in);
int finish = inputSn.nextInt();

int answer = (start + finish);
System.out.println(answer);


}
}

1 个答案:

答案 0 :(得分:0)

if

if (inputFn.hasNextInt()){
}else{
  int start = inputFn.nextInt();
  System.out.print("Play By The Rules And Enter An Integer");
}

有点倒退。只有在没有整数时才读取整数。而且你在if块中定义了start变量,这就是你得到错误的原因。

更像是

int start = 0;
if (inputFn.hasNextInt()){
  start = inputFn.nextInt();
}else{
  System.out.print("Play By The Rules And Enter An Integer");
}

似乎更好。