无法尝试重启我的Java程序

时间:2013-08-28 11:47:19

标签: java

在查找了许多重启Java程序的方法之后,while循环似乎是最简单的选择。以下是我正在尝试使用的基本计算器程序示例:

import java.util.Scanner;
class a {
public static void main(String args[]){
    boolean done = false;
    int oper;
    Scanner input = new Scanner(System.in);
    System.out.println("McMackins Calc v2.0 (Now with fewer crashes!)");
    while (!done)
    {
    System.out.println("What operation? (0 for quit, 1 for add, 2 for subtract, 3 for multiply, 4 for divide, 5 for divide with remainder, 6 for average, 7 for account interest):");
    while (!input.hasNextInt()){
        System.out.println("Enter a valid integer.");
        input.next();
    }
    oper = input.nextInt();
    switch (oper){
    case 0:
        done = true;
        break;
    case 1:
        add addObject = new add();
        addObject.getSum();
        break;
    case 2:
        sub subObject = new sub();
        subObject.getDifference();
        break;
    case 3:
        times multObject = new times();
        multObject.getProduct();
        break;
    case 4:
        divide divObject = new divide();
        divObject.getQuotient();
        break;
    case 5:
        remain remObject = new remain();
        remObject.getRemainder();
        break;
    case 6:
        avg avgObject = new avg();
        avgObject.getAvg();
        break;
    case 7:
        interest intObject = new interest();
        intObject.getInterest();
        break;
    default:
        System.out.println("Invalid entry.");
        break;
    }
    }
    input.close();
}
}

但是,这似乎在第一次循环结束时抛出NoSuchElementException,并导致程序崩溃。此类的功能是从用户获取初始输入以确定要使用哪个类,这将确定要执行的数学运算。没有while (!done)循环,一切正常。

使用示例:

McMackins Calc v2.0 (Now with fewer crashes!)
What operation? (0 for quit, 1 for add, 2 for subtract, 3 for multiply, 4 for divide, 5 for divide with remainder, 6 for average, 7 for account interest):
1
How many addends?
1
Enter your numbers now.
1
You have entered 1 addend.
The sum is: 1.0
What operation? (0 for quit, 1 for add, 2 for subtract, 3 for multiply, 4 for divide, 5 for divide with remainder, 6 for average, 7 for account interest):
Enter a valid integer.
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at a.main(a.java:13)

我也试过让其他类引用回来,但由于main是一个静态方法,我不能按照我的意图访问它。

请注意,我是Java的初学者,这就是为什么我的程序非常简单,所以尽量保持简单,如果它可以,或者发布代码,然后在DETAIL中解释它意味着什么,所以我可以不仅解决了这个问题,也解决了未来的问题。

谢谢!

编辑:

我的编辑器中的代码格式更好。当我在这里发布时,括号出现在奇怪的位置。

由于显然正确地写了a,这是我的add类。希望这会清除一些东西。

import java.util.Scanner;
public class add {
public void getSum(){
    Scanner input = new Scanner(System.in);
    double total, addend;
    int entries, count;
    total = 0;
    count = 0;
    System.out.println("How many addends?");
    while (!input.hasNextInt()){
        System.out.println("Enter a valid integer.");
        input.next();
    }
    entries = input.nextInt();
    System.out.println("Enter your numbers now.");

    while (count < entries){
        while (!input.hasNextDouble()){
            System.out.println("Enter a valid number.");
            input.next();
        }
        addend = input.nextDouble();
        total = total + addend;
        count++;
        if (count == 1){
            System.out.println("You have entered " + count + " addend.");
        }else if (count > entries){
            System.out.println("You have entered too many addends! Contact program developer.");
        }else{
            System.out.println("You have entered " + count + " addends.");
        }
    }
    System.out.println("The sum is: " + total);
    input.close();
}
}

3 个答案:

答案 0 :(得分:2)

public static void main(String args[]){
    boolean done = false;
    int oper;
    Scanner input = new Scanner(System.in);
    System.out.println("McMackins Calc v2.0 (Now with fewer crashes!)");

    while (!done) {
        System.out.println("What operation? (0 for quit, 1 for add, 2 for subtract, 3 for multiply, 4 for divide, 5 for divide with remainder, 6 for average, 7 for account interest):");
        while (!input.hasNextInt()){
            System.out.println("Enter a valid integer.");
            input.next();
        }
        oper = input.nextInt();
        switch (oper){
        case 0:
            done = true;
            break;
        case 1:
            System.out.println("1");
            break;
        case 2:
            System.out.println("2");
            break;
        case 3:
            System.out.println("3");
            break;
        case 4:
            System.out.println("4");
            break;
        case 5:
            System.out.println("5");
            break;
        case 6:
            System.out.println("6");
            break;
        case 7:
            System.out.println("7");
            break;
        default:
            System.out.println("Invalid entry.");
            break;
        }
    }
    input.close();
}

这似乎对我有用,所以错误可能与你自己的类(添加,划分)等有关。

此外,最好通过大写第一个字母来创建自己的类时遵守约定,例如“添加”应为“添加”。

通过构建一个包含add方法,减法方法等的通用“Operations”类,你可以更容易阅读它。

修改 试试这个添加方法:

public static int add() {
        Scanner s = new Scanner(System.in);
        int counter = 0;

        System.out.println("How many numbers to add?");
        int numCount = s.nextInt();

        for(int i = 0; i < numCount; i++) {
            System.out.println("enter number");
            counter += s.nextInt();
        }

        return counter;
    }

答案 1 :(得分:1)

使用bufferedreaderinputstream代替Scanner类。这个类会产生很多错误和错误,因为有时需要更多的参数,你期望它采取。 也:

while (!input.hasNextInt()){
    System.out.println("Enter a valid integer.");
    input.next();
}

您使用hasNextInt方法时出错,而不是尝试使用布尔进行简单的while循环,input.next()应替换为input.nextLine()

另一件事,你应该检查,如果用户输入整数而不是字符串或while循环及其范围内的某些东西。如果一切正常,你应该将布尔值更改为true并让他离开while循环。

答案 2 :(得分:1)

对于想知道如何解决这个问题的未来用户,通过一些重新编程,我发现我的问题是在循环结束前关闭input变量。通过让程序无限期地重启并且只在完成时关闭input,该程序可以正常工作。

感谢Benjamin的回复,我目前正在通过for循环清理和缩短代码。