我正在尝试编写一个带有2个函数的简单计算器(现在用)。我在Eclipse中使用Java。我无法得到我目前正在努力工作的东西。我希望只有菜单项作为自己的功能,然后在接受entryChoice后将case切换为自己的功能。当我按原样运行此代码并进行选择时,它所做的只是重复“输入两个数字......”或者,如果我将userInput分隔成在entryChoice传递后要打印的每个案例,它就会中断。有什么建议吗?
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;
}
}
答案 0 :(得分:0)
我认为你想要的是以下内容:
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;
}
答案 1 :(得分:0)
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) {
System.out.println(userSelection(entryChoice));
displayMenu();
entryChoice = scanChoice.nextInt();
}
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) {
Scanner userInput = new Scanner(System.in);
double x = 0;
double y = 0;
double result = 0;
if (entryChoice == 6) {
System.out.println("Enter one number: ");
x = userInput.nextDouble();
} else {
System.out.println("Enter two numbers seperated by a space");
x = userInput.nextDouble();
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:
result = Math.sqrt(x);
break;
case 7:
result = 0;
break;
default:
}
return result;
}
答案 2 :(得分:0)
请尝试这个,它按照您的期望工作
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;
}
}