如何将开关盒返回到主程序

  • 本文关键字:返回 主程序 开关 java
  • 更新时间 :
  • 英文 :


我的程序在用户输入距离和它通过的介质(空气,水,土,钢)后计算声速。我只需要知道如何在计算完成后返回我的主程序,如果用户输入 yes 或停止程序(我假设中断),如果他们输入 no。

System.out.print(" What is the distance in feet:" );
//ask the user to input variables
while (!keyboard.hasNextDouble()) {
    System.out.println("Please enter a valid numeric value, try again: ");
    keyboard.next();
}
Distance =keyboard.nextDouble();
{
    System.out.print("Input the media: Air, Water, Steel, or Earth: ");
    String Input = keyboard.next();
    switch(Input.toLowerCase()) {
        case "air":
            AirSpeed = Distance/Air;
            System.out.print("n nThe time to for sound to travel ");
            System.out.print(Distance);
            System.out.print(" feet through AIR" +"n");
            System.out.printf("%.6f", AirSpeed);
            System.out.print(" seconds or ");
            System.out.printf("%.1f", OneFootPerSecond*Air);
            System.out.print(" miles per hour."); 
            System.out.print("n nEnter Yes for another calculation, else No: ");
            String Another = keyboard.next();
            Another.toLowerCase();
            break;
        case "water":
            WaterSpeed = Distance/Water;
            System.out.print("nThe time to for sound to travel ");
            System.out.print(Distance);
            System.out.print(" feet through WATER" +"n");
            System.out.printf("%.6f",WaterSpeed);
            System.out.print(" seconds or ");
            System.out.printf("%.1f", OneFootPerSecond*Water);
            System.out.print(" miles per hour."); 
            System.out.print("n nEnter Yes for another calculation, else No: ");
            Another = keyboard.next();
            Another.toLowerCase();
            break;
        case "steel":
            SteelSpeed = Distance/Steel;
            System.out.print("nThe time to for sound to travel ");
            System.out.print(Distance);
            System.out.print(" feet through STEEL" +"n");
            System.out.printf("%.6f",SteelSpeed);
            System.out.print(" seconds or ");
            System.out.printf("%.1f", OneFootPerSecond*Steel);
            System.out.print(" miles per hour."); 
            System.out.print("n nEnter Yes for another calculation, else No: ");
            String Another = keyboard.next();
            Another.toLowerCase();
            break;
        case "earth":
            EarthSpeed = Distance/Water;
            System.out.print("nThe time to for sound to travel ");
            System.out.print(Distance);
            System.out.print(" feet through EARTH" +"n");
            System.out.printf("%.6f",EarthSpeed);
            System.out.print(" seconds or ");
            System.out.printf("%.1f", OneFootPerSecond*Earth);
            System.out.print(" miles per hour."); 
            System.out.print("n nEnter Yes for another calculation, else No: ");
            String Another = keyboard.next();
            Another.toLowerCase();
            break;
        // TODO code application logic here
        default :
            System.out.print("Invalid. Re-run the program. ");
            break;
    }
} 

我收集到,如果用户表示他们想要继续,您希望继续重复 switch 语句。在这种情况下,您需要将 switch 语句包装在一个循环中,该循环一直循环,直到用户说"否"。请考虑以下示例:

boolean shouldContinue = true;
while (shouldContinue == true){
    switch(Input.toLowerCase()) {
        case "earth":
            //calculate speed...
            System.out.print("n nEnter Yes for another calculation, else No: ");
            String Another = keyboard.next();
            Another.toLowerCase();
            //If the user inputs 'no', we change shouldContinue, that stops the loop from executing again
            if (Another.equals("no"){
                shouldContinue = false;
            }
        break;

         case "water":
            //calculate speed...
            System.out.print("n nEnter Yes for another calculation, else No: ");
            Another = keyboard.next();
            Another.toLowerCase();
            //If the user inputs 'no', we change shouldContinue, that stops the loop from executing again
            if (Another.equals("no"){
                shouldContinue = false;
            }
        break;
         case "steel":
            //calculate speed...
            System.out.print("n nEnter Yes for another calculation, else No: ");
            String Another = keyboard.next();
            Another.toLowerCase();
            //If the user inputs 'no', we change shouldContinue, that stops the loop from executing again
            if (Another.equals("no"){
                shouldContinue = false;
            } 
           break;     
        case "earth":
            //calculate speed...
            System.out.print("n nEnter Yes for another calculation, else No: ");
            String Another = keyboard.next();
            Another.toLowerCase();
            //If the user inputs 'no', we change shouldContinue, that stops the loop from executing again
            if (Another.equals("no"){
                shouldContinue = false;
            } 
       break;
        default :
            System.out.print("Invalid. Re-run the program. ");
            shouldContinue = false;    
        break;
   }
}

关于代码的其他一些说明:

您没有遵循变量的 java 命名约定。所有变量名称都应以小写字母开头。这是一个简单的外观更改,使您的代码更易于阅读。

除变量外,所有实例、类和类常量都在 首字母小写的混合大小写

可以说,你也把太多的代码放在一个地方,你应该尝试通过将其中一些代码移动到单独的(可重用的)方法来改进你的代码。

您需要从某种循环内部调用该方法(其中包含开关)。

例如
public static void main() {

    //start loop
    //invoke method containing switch statement.
    //endloop
    }

否则,您的开关被执行,然后什么也无事可做,程序结束。

也许尝试:

while(true) {
  //invoke switch code containing switch statement
}

您必须手动强制退出,然后如果它执行您想要的操作,请尝试使其在命令上优雅地退出(不想暗示太多)。

最新更新