如何将Switch-Case返回主程序

时间:2013-09-28 17:25:35

标签: 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;
    }
} 

2 个答案:

答案 0 :(得分:1)

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

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 naming conventions。所有变量名称都应以小写字母开头。这是一个简单的化妆品更改,使您的代码更容易阅读。

  

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

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

答案 1 :(得分:-1)

您需要从某种循环内部调用该方法(使用其中的开关)。 e.g。

public static void main() {


    //start loop

    //invoke method containing switch statement.

    //endloop

    }

否则你执行切换,然后没有什么可做的,程序结束。

也许试试:

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

你必须手动强制退出,然后如果你做了你想做的事情,试着让它在命令上优雅地退出(不想暗示太多)。