程序以完全相同的输入值无法预测地运行

时间:2016-10-31 05:36:31

标签: java if-statement output

我是编程和java的新手,并且有一个我已经能够正常运行的作业 - 有时,但它是不一致的。我用一定的值运行它一次,然后直接尝试使用与之前完全相同的输入重新运行它。下一次左右它不起作用。请指点我如何解决这种不可靠性。我在代码中找不到任何理由。请帮忙。

import java.util.*; 
public class Calculator {

    public static void main(String[] args) { //void
        boolean done = false;
        Scanner console = new Scanner(System.in);
        while (!done){
            displayMenu();
            String selection = getUsersSelection(console);
            done = processSelection(selection, console);
            //caluculateResults(console);
        }
        System.out.println("Thank you for using this program");
    }

    private static boolean processSelection(String selection, Scanner console) {
        boolean done = false;
        if (!selection.equalsIgnoreCase("Q")){
            if (selection.equalsIgnoreCase("U")){
                caluculateResults(console);
                }
            else if (selection.equalsIgnoreCase("H")){

            }
            else {
                System.out.println("Incorrect entry...try again!");
            }
        }
        else {
            done = true;
        }
        return done;
    }

    private static void caluculateResults(Scanner console) {
        displayCalculatorInstructions();
        double operand1 = console.nextDouble();
        char operator = console.next().charAt(0);
        double operand2 = console.nextDouble();
        double result = 0.00; //double from int
        boolean isOperatorValid = true;
        if (operator == '+'){
            result = operand1 + operand2;
        }
        else if (operator == '-'){
            result = operand1 - operand2; 
}
        else if (operator == '*'){
            result = (double) operand1 * (double) operand2;
        }
        else if (operator == '/'){
            if (operand2 != 0.00){
                result = operand1 / operand2;
            }
            else {
                result = (int)Double.NaN;
            }
        }
        else if (operator == '^'){
            result = Math.pow(operand1, operand2);
        }
        else {
            isOperatorValid = false;
            System.out.println("Invalid operator");
}
        if (isOperatorValid){
            System.out.println("The result of your operation is: ");
            System.out.printf(operand1 +" "+operator+" "+operand2+" = %5.3f", result); 
            System.out.println();
        }
    }

    private static void displayCalculatorInstructions() {
        System.out.println("Enter a mathematical expression to evaluate");
        System.out.println("Valid operations are: +, -, /, *, ^ for power");
        System.out.println("Expression are entered with spaces between the values and operator");
        System.out.println("Here is the valid format:");
        System.out.println("\t<value><space><operator><space><value>");
        System.out.print("Your expression: ");
    }


    private static String 
    private static void displayMenu() {
        System.out.println("Enter one these options:");
        System.out.println("\tH for Help");
        System.out.println("\tU for using calculator");
        System.out.println("\tQ for exiting this program");
        System.out.print("Your selection: ");getUsersSelection(Scanner console) {
        String selection = console.next();
return selection;
    }

    }

}

1 个答案:

答案 0 :(得分:0)

我相信上面提供的代码不会在没有一些更改的情况下编译。而且我确信你在某处得到了错误的输入,因为我从来没有从你的caluculateResults方法得到不同的结果。

主要问题在于您提供的displayMenu方法

private static String 
private static void displayMenu() {
    System.out.println("Enter one these options:");
    System.out.println("\tH for Help");
    System.out.println("\tU for using calculator");
    System.out.println("\tQ for exiting this program");
    System.out.print("Your selection: ");getUsersSelection(Scanner console) {
    String selection = console.next();
  return selection;
}

}

这有几个问题......首先编译器会警告缺少“;”after private static String。然后,System.out.print("Your selection: ");getUsersSelection(Scanner console) {行看起来并不正确。您似乎正在尝试选择用户输入?那就明白吧?最后,对于这个方法,似乎你想要返回一个名为selection的字符串变量,但是该方法没有签名来返回一个字符串,而且在main方法中,如果确实如此,你就不会捕获它。在这种情况下,我将假设您要返回String。 以下是我对此方法所做的更改。

private static String displayMenu(Scanner console)
{
  System.out.println("Enter one these options:");
  System.out.println("\tH for Help");
  System.out.println("\tU for using calculator");
  System.out.println("\tQ for exiting this program")
  System.out.print("Your selection: ");
  String selection = console.next();
  return selection;
}

在你的主要方法......

public static void main(String[] args)
{
    boolean done = false;
    Scanner console = new Scanner(System.in);
    while (!done){
        displayMenu();
        String selection = getUsersSelection(console);
        done = processSelection(selection, console);
        //caluculateResults(console);
    }
    System.out.println("Thank you for using this program");
}

主要需要进行微小的改动。首先,displayMenu()应该返回一个你没有捕获的字符串。由于你没有捕获它,你基本上要求用户再次输入,因为displayMenu()只是要求用户输入,但你没有得到它。所以让我们从displayMenu()获取用户输入,因为我们现在用String selection = displayMenu(console);返回字符串然后只删除行String selection = getUsersSelection(console);这就是我认为你输入错误的地方,因为你的方法calculateResults从未给出我的测试结果不同。

public static void main(String[] args)
{
  boolean done = false;
  Scanner console = new Scanner(System.in);
  while (!done)
  {
    String selection = displayMenu(console);
    done = processSelection(selection, console);
    System.out.println("");
  }
  System.out.println("Thank you for using this program");
}

希望这有帮助。