Java 简单计算器的菜单功能和案例切换功能有问题

Java 简单计算器的菜单功能和案例切换功能有问题,java,menu,switch-statement,case,calculator,Java,Menu,Switch Statement,Case,Calculator,我现在正试着写一个简单的计算器,有两个函数。我在Eclipse中使用Java。我无法获得我当前正在尝试的工作。我只想让菜单项作为它自己的功能,然后在输入entryChoice之后,case开关成为它自己的功能。当我按原样运行此代码并进行选择时,它所做的只是重复输入两个数字。。。或者,如果在entryChoice被传递后,我将userInput分离到每个要打印的案例中,它就会中断。有什么建议吗 public static void main(String[] args) { // TODO

我现在正试着写一个简单的计算器,有两个函数。我在Eclipse中使用Java。我无法获得我当前正在尝试的工作。我只想让菜单项作为它自己的功能,然后在输入entryChoice之后,case开关成为它自己的功能。当我按原样运行此代码并进行选择时,它所做的只是重复输入两个数字。。。或者,如果在entryChoice被传递后,我将userInput分离到每个要打印的案例中,它就会中断。有什么建议吗

public static void main(String[] args) {
    // TODO Auto-generated method stub
    displayMenu();

    Scanner scanChoice = new Scanner(System.in);
    int entryChoice = scanChoice.nextInt();

    while (entryChoice != 7)
    {
        userSelection(entryChoice);
    }

    System.exit(0); 
}

public static void displayMenu()
{
    System.out.println("Please select from the following choices:");
    System.out.println();

    System.out.println("1) Addition");
    System.out.println("2) Subtraction");
    System.out.println("3) Multiplication");
    System.out.println("4) Division");
    System.out.println("5) Raise to a Power");
    System.out.println("6) Square Root");
    System.out.println("7) Exit Program");

    System.out.println();
    System.out.println("Enter your choice here: ");

}

public static double userSelection(int entryChoice)
{
    double result = 0;
    System.out.println("Enter two numbers seperated by a space");
    Scanner userInput = new Scanner(System.in);

    double x = userInput.nextDouble();
    double y = userInput.nextDouble();

    switch (entryChoice)
    {

        case 1:
            result = x + y;
            break;

        case 2:
            result = x - y;
            break;

        case 3:
            result = x * y;
            break;

        case 4:
            result = x / y;
            break;

        case 5:
            result = Math.pow(x,y);
            break;

        case 6:
            System.out.println("Enter one number: "); // some kinks to work out here..
            result = Math.sqrt(x);
            break;

        case 7:
            result = 0;
            break;
        default:

    }

    return result;
}

}我想你想要的是:

public static void main(String[] args) {
    // TODO Auto-generated method stub

    int entryChoice = -1;
    while (entryChoice != 7)
    {
        displayMenu();

        Scanner scanChoice = new Scanner(System.in);
        entryChoice = scanChoice.nextInt();
        if (entryChoice != 7)
            System.out.println(userSelection(entryChoice));
    }

    System.exit(0); 
}

public static void displayMenu()
{
    System.out.println("Please select from the following choices:");
    System.out.println();

    System.out.println("1) Addition");
    System.out.println("2) Subtraction");
    System.out.println("3) Multiplication");
    System.out.println("4) Division");
    System.out.println("5) Raise to a Power");
    System.out.println("6) Square Root");
    System.out.println("7) Exit Program");
    System.out.println();
    System.out.println("Enter your choice here: ");


}

public static double userSelection(int entryChoice)
{
    double result = 0;
    System.out.println("Enter two numbers seperated by a space");
    Scanner userInput = new Scanner(System.in);

    double x = userInput.nextDouble();
    double y = userInput.nextDouble();

    switch (entryChoice)
    {

        case 1:
            result = x + y;
            break;

        case 2:
            result = x - y;
            break;

        case 3:
            result = x * y;
            break;

        case 4:
            result = x / y;
            break;

        case 5:
            result = Math.pow(x,y);
            break;

        case 6:
            System.out.println("Enter one number: "); // some kinks to work out here..
            result = Math.sqrt(x);
            break;

        default:

    }

    return result;
}

请试试这个,它正按照您的期望工作

import java.util.*;
class Calculator
{
public static void main(String[] args) {
    // TODO Auto-generated method stub
    displayMenu();
}

public static void displayMenu()
{
    System.out.println("Please select from the following choices:");
    System.out.println();

    System.out.println("1) Addition");
    System.out.println("2) Subtraction");
    System.out.println("3) Multiplication");
    System.out.println("4) Division");
    System.out.println("5) Raise to a Power");
    System.out.println("6) Square Root");
    System.out.println("7) Exit Program");

    System.out.println();
    System.out.println("Enter your choice here: ");
    Scanner scanChoice = new Scanner(System.in);
    int entryChoice = scanChoice.nextInt();

    while (entryChoice != 7)
    {
        userSelection(entryChoice);
     }

    System.exit(0); 
}

public static double userSelection(int entryChoice)
{
    double result = 0;
    System.out.println("Enter two numbers seperated by a space");
    Scanner userInput = new Scanner(System.in);

    double x = userInput.nextDouble();
    double y = userInput.nextDouble();

    switch (entryChoice)
    {

        case 1:
            result = x + y;
        System.out.println("Result is :"+result);
            break;

        case 2:
            result = x - y;
        System.out.println("Result is :"+result);
            break;

        case 3:
            result = x * y;
        System.out.println("Result is :"+result);
            break;

        case 4:
            result = x / y;
        System.out.println("Result is :"+result);
            break;

        case 5:
            result = Math.pow(x,y);
        System.out.println("Result is :"+result);
            break;

        case 6:
            System.out.println("Enter one number: "); // some kinks to work out here..
            result = Math.sqrt(x);
        System.out.println("Result is :"+result);
            break;

        case 7:
            result = 0;
            break;
        default:

    }
displayMenu();

    return result;
}
}
import java.util.*;
class Calculator
{
public static void main(String[] args) {
    // TODO Auto-generated method stub
    displayMenu();
}

public static void displayMenu()
{
    System.out.println("Please select from the following choices:");
    System.out.println();

    System.out.println("1) Addition");
    System.out.println("2) Subtraction");
    System.out.println("3) Multiplication");
    System.out.println("4) Division");
    System.out.println("5) Raise to a Power");
    System.out.println("6) Square Root");
    System.out.println("7) Exit Program");

    System.out.println();
    System.out.println("Enter your choice here: ");
    Scanner scanChoice = new Scanner(System.in);
    int entryChoice = scanChoice.nextInt();

    while (entryChoice != 7)
    {
        userSelection(entryChoice);
     }

    System.exit(0); 
}

public static double userSelection(int entryChoice)
{
    double result = 0;
    System.out.println("Enter two numbers seperated by a space");
    Scanner userInput = new Scanner(System.in);

    double x = userInput.nextDouble();
    double y = userInput.nextDouble();

    switch (entryChoice)
    {

        case 1:
            result = x + y;
        System.out.println("Result is :"+result);
            break;

        case 2:
            result = x - y;
        System.out.println("Result is :"+result);
            break;

        case 3:
            result = x * y;
        System.out.println("Result is :"+result);
            break;

        case 4:
            result = x / y;
        System.out.println("Result is :"+result);
            break;

        case 5:
            result = Math.pow(x,y);
        System.out.println("Result is :"+result);
            break;

        case 6:
            System.out.println("Enter one number: "); // some kinks to work out here..
            result = Math.sqrt(x);
        System.out.println("Result is :"+result);
            break;

        case 7:
            result = 0;
            break;
        default:

    }
displayMenu();

    return result;
}
}