代码有效且非常简单。我用一个开关做了另一个,但老师想要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);
}
}
}
答案 0 :(得分:0)
您需要在整个块周围使用while
循环。
然后
else if(menuSelect.equals("h"))
{
break;
}
这将打破循环