我有这个程序,这是我的方法之一。但是我有一个语法错误,它的说法是抛出预期,但我不想添加一个throw ..如何解决这个问题?
public static void enterID(int[] list, int size){
System.out.println("Enter Employee ID#");
Scanner input = new Scanner(System.in);
for(int i = 0; i < size; i++){
int hours = input.nextInt();
list[i] = hours;
if(hours < 0)||(hours > 40); //throwsException
{
System.out.println("INVALID! Should be positive. REENTER: ");
list[i] = input.nextInt();
}
}
}
答案 0 :(得分:0)
在您的代码中
if(hours < 0)||(hours > 40);
哪个应该是
if(hours < 0 || hours > 40){
//code here
}
放一个;在if
语句的末尾不允许应该在if语句中正确执行的代码块。此外,if(hours < 0)||(hours > 40)
语法无效,应为if(hours < 0 || hours > 40)
正确的方法如下:
public static void enterID(int[] list, int size){
System.out.println("Enter Employee ID#");
Scanner input = new Scanner(System.in);
for(int i = 0; i < size; i++){
int hours = input.nextInt();
list[i] = hours;
if(hours < 0 || hours > 40)
{
System.out.println("INVALID! Should be positive. REENTER: ");
list[i] = input.nextInt();
}
}
}
答案 1 :(得分:0)
我认为你必须删除分号;在if
if(hours < 0)||(hours > 40);
到
if(hours < 0)||(hours > 40)
{
System.out.println("INVALID! Should be positive. REENTER: ");
list[i] = input.nextInt();
}