簡體   English   中英

帶Switch語句的While循環

[英]While Loop with Switch Statement

我無法讓開關不斷循環直到用戶告訴程序停止為止。 當提示用戶輸入N進行循環時,應將其發送回開關的頂部

        char stop;
    while(stop == 'N'){
        switch(choice){
            case 1:
                System.out.println("Enter the time in seconds:");
                time = input.nextInt();
                displacement = (Math.pow(time,4)) + 16;
                System.out.println("Felix's displacement is equal to " + displacement + " meters");
                System.out.println("Stop the application(Y/N)");
                stop = input.findWithinHorizon(".",0).charAt(0);
                break;
            case 2:
                System.out.println("Enter the time in seconds:");
                time = input.nextInt();
                velocity = 4*(Math.pow(time,3));
                System.out.println("Felix's velocity is equal to " + velocity + " m/s");
                break;
            case 3:
                System.out.println("Enter the time in seconds:");
                time = input.nextInt();
                acceleration = 12*(Math.pow(time,2));
                System.out.println("Felix's acceleration is equal to " + acceleration + " m/(s*s)");
                break;
            default:
                System.out.println("Please select a choice");

        }            
    }
}
  1. 粗略: 初始化 stop聲明:

char stop = 'N';

  1. 更好:將您的while替換為do while循環:

    做{

    } while(stop =='N')

當您在第一次通過while循環引用它時,“停止”指的是內存中的空白空間,因此它將永遠不會開始。 您需要對其進行初始化或重新排列循環結構。 此外,似乎您需要提示用戶“選擇”,除非已刪除該部分代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM