该代码有效并且非常简单。我使用开关做了另一个,但老师想要 if 语句。因此,用户输入 2 个数字,然后从我创建的 println 菜单中进行选择。我需要用户能够做出第二选择。比如选择“a”进行加法,之后也可以选择“b”减法。在做出每个选择之后,如果能够让菜单重现,那就太好了。我需要第一个用户输入的数字是持久的。
谢谢。编辑:这是有效的最终版本
import java.util.Scanner;
public class Assign3
{
public static void main (String [] args)
{
double numOne;
double numTwo;
String answerTo = null;
System.out.println("============================");
System.out.println("| MENU Calculator Fun |");
System.out.println("============================");
System.out.println("| Options: |");
System.out.println("| a. Add |");
System.out.println("| b. Substract |");
System.out.println("| c. Multiply |");
System.out.println("| d. Divide |");
System.out.println("| e. Mod |");
System.out.println("| f. Compare |");
System.out.println("| g. Print Numbers |");
System.out.println("| h. Quit |");
System.out.println("============================");
Scanner numFinder = new Scanner(System.in);
System.out.println("Please enter a number then press enter then enter another number followed by enter.");
numOne = numFinder.nextDouble();
numTwo = numFinder.nextDouble();
while(true)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please choose an option.");
String menuSelect = scan.next();
if(menuSelect.length() > 1)
{
System.out.println("Error, too many letters...");
}
else if(menuSelect.equals("a"))
{
answerTo = Double.toString(numOne +numTwo);
}
else if(menuSelect.equals("b"))
{
answerTo = Double.toString(numOne - numTwo);
}
else if(menuSelect.equals("c"))
{
answerTo = Double.toString(numOne * numTwo);
}
else if(menuSelect.equals("d"))
{
answerTo = Double.toString(numOne / numTwo);
}
else if(menuSelect.equals("e"))
{
answerTo = Double.toString(numOne % numTwo);
}
else if (menuSelect.equals("f"))
if(numOne > numTwo)
{
answerTo = ">";//System.out.println("First is grater");
}
else if(numOne < numTwo)
{
answerTo ="<";//System.out.println("Second is grater");
}
else
{
answerTo ="=";//System.out.println("Both are equal");
}
else if(menuSelect.equals("g"))
{
answerTo = "First number: " + numOne + " Second number: " + numTwo;
}
else if(menuSelect.equals("h"))
{
break;
}
System.out.println("answer: " + answerTo);
}
}
}