Понимание ошибки «имя не существует в текущем контексте»

Инкапсулируйте его в методе - что-то вроде ниже. И затем просто используйте метод для извлечения целых чисел от пользователя.

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;    
}

EDIT:

Это полное решение, если вы идете с этим подходом к блоку 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;
    }

}
-9
задан Joel 5 February 2016 в 23:37
поделиться