1

我的程序运行良好,除此之外用户仍然可以输入会破坏程序的字符串/字符,从而导致错误。

我想写一个语句,以便当用户输入除 0-100 之间的整数以外的任何内容时,将显示一条错误消息(基本上我的字符串说他们不能这样做),或者最好他们不能输入除一个之外的任何内容整数(如果可能)。

这是我的代码:

    int[] cw_Mark = new int[6];
    for (int i = 0; i < cw_Mark.length; i++) {
        if (i >= 0 || i <= 100) {
            System.out.println("Please Enter Your Module " + (i+1) + " Coursework Mark: ");
            cw_Mark[i] = input.nextInt();
        } else {
            System.out.println("Please Enter a Number Between 1 and 100");
        }
    }
4

4 回答 4

1

将其封装在一个方法中 - 如下所示。然后只需使用该方法从用户那里获取整数。

public static int getInt() {

    System.out.print("Please enter an integer");
    if (console.hasNext("stop")) {
        System.out.println("***Terminated***");
        System.exit(0);
    }
    while(!console.hasNextInt()) {
        System.out.print("Input is not a valid integer!");
        console.next();
    }
    int temp = console.nextInt();
    while(temp<0 && temp>100) {
        System.out.println("invalid input");
        temp = console.nextInt();
    }
    return temp;    
}

编辑:

如果使用这种方法而不是 try-catch 块,这就是完整的解决方案。

package standard;

import java.util.Scanner;
public class Main {
    static Scanner console = new Scanner(System.in);
    public static void main(String[] args) {

        int[] cw_Mark = new int[6];
        for (int i = 0; i < cw_Mark.length; i++) {
            System.out.println("Please Enter Your Module " + (i + 1)
                    + " Coursework Mark: ");
            cw_Mark[i] = getInt();
        }
    }

    //Gets the next integer from the console. 
    public static int getInt() {

        System.out.print("Please enter an integer");
        if (console.hasNext("stop")) {
            System.out.println("***Terminated***");
            System.exit(0);
        }
        while (!console.hasNextInt()) {
            System.out.print("Input is not an integer!");
            console.next();
        }
        int temp = console.nextInt();
        while (temp <= 0 && temp >= 100) {
            System.out.println("Input must be between 0 and 100");
            temp = console.nextInt();
        }
        return temp;
    }

}
于 2013-11-06T17:13:23.233 回答
0

这应该这样做:

while(true)
{
    try {
        System.out.println("Please Enter Your Module " + (i+1) + " Coursework Mark: ");
        cw_Mark[i] = input.nextInt();
        if(cw_Mark[i}>=0&&cw_Mark[i]<=100)
            break;
    } catch(InputMismatchException e) {
         System.out.println("Needs a number.");
    }
    System.out.println("Needs a number from 1 to 100");
}

这将捕获任何错误的值并不断提示您的用户为您提供正确的值,直到他们这样做为止。

编辑:为了澄清,您只需将其粘贴到您当前拥有的位置

System.out.println("Please Enter Your Module " + (i+1) + " Coursework Mark: ");
        cw_Mark[i] = input.nextInt();

并取出您的 0 到 100 的支票。它在那里没有任何好处,它没有在正确的位置进行检查以产生影响。

于 2013-11-06T17:18:17.500 回答
0

尝试这个

    int[] cw_Mark = new int[6];
    for (int i = 0; i < cw_Mark.length; i++) {
        System.out.println("Please Enter Your Module " + (i + 1) + " Coursework Mark: ");
        while (true) {
            String next = input.next();
            try {
                cw_Mark[i] = Integer.parseInt(next);
                if (i >= 0 || i <= 100) {
                    break;
                }
            } catch (NumberFormatException e) {
            }
            System.out.println("Please Enter a Number Between 1 and 100");
        }
    }
于 2013-11-06T17:19:15.210 回答
0

为了避免程序退出抛出异常,您可以简单地使用 try/catch 包装用户输入接受语句:

try {
    cw_Mark[i] = input.nextInt();
} catch(InputMismatchException e) {
     System.out.println("Entered value is not a number");
}
于 2013-11-06T17:11:08.283 回答