use*_*515 5 java switch-statement
好的,我知道我之前问过,但我比我的更进一步.所以这是我的问题.
我必须编写一个程序,它将从用户读取两个数字(双重类型).然后程序应该向用户显示一个选项菜单,允许他们将第一个数字加上,乘以或除以第二个数字.我的程序应该将零除以并将错误报告给用户.
以下是我到目前为止我只是在将数学部分放在开关语句中的哪个方面丢失了
import java.util.*;
public class tester
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
double MyDouble1;
double MyDouble2;
System.out.print(" Please enter the first decimal number: ");
MyDouble1 = console.nextDouble();
System.out.print(" Please enter the second decimal number: ");
MyDouble2 = console.nextDouble();
// Display menu graphics
System.out.println("============================");
System.out.println("| MENU SELECTION DEMO |");
System.out.println("============================");
System.out.println("| Options: |");
System.out.println("| 1. Addition |");
System.out.println("| 2. Multiply |");
System.out.println("| 3. Divide |");
System.out.println("| 4. Exit |");
System.out.println("============================");
MyDouble1 = console.nextDouble();
System.out.print(" Select option: ");
// Switch construct
switch (MyDouble1)
{
case 1:
System.out.println("Addition selected");
break;
case 2:
System.out.println("Multiply selected");
break;
case 3:
System.out.println("Divide selected");
break;
case 4:
System.out.println("Exit selected");
break;
default:
System.out.println("Invalid selection");
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我寻找其他的tuts但我不知道我是否在寻找合适的地方.任何帮助都会很好,谢谢.
将代码放在case中,还需要一个变量choice来进行操作。
switch (choice) //choice has to be int, byte, short, or char (String with java 7)
{
case 1:
// Your code goes here
System.out.println("Addition selected");
break;
case 2:
System.out.println("Multiply selected");
break;
case 3:
System.out.println("Divide selected");
break;
case 4:
System.out.println("Exit selected");
break;
default:
System.out.println("Invalid selection");
break;
}
Run Code Online (Sandbox Code Playgroud)