-1

在查找了许多在自身内部重新启动 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();
}
}
4

3 回答 3

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”应该是“Add”。

您可以通过构建一个包含加法、减法等的通用“操作”类来使其更易于阅读。

编辑: 试试这个添加方法:

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;
    }
于 2013-08-28T12:02:21.040 回答
1

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

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

于 2013-08-29T20:40:39.457 回答
1

使用bufferedreaderandinputstream代替 Scanner 类。这个类会产生很多错误和错误,因为有时它需要更多的参数,而你期望它会接受。还:

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

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

另一件事,您应该检查用户是否在 while 循环中键入整数而不是字符串或其他内容,并且它的范围。如果一切正常,您应该将布尔值更改为 true 并让他退出 while 循环。

于 2013-08-28T11:58:18.887 回答