2015-03-31 158 views
1

我無法讓開關不斷循環,直到用戶告訴程序停止。 當用戶提示放在一個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

進入循環時停止的值是多少?它沒有設置,因此它不會進入循環。什麼是選擇發送給?你根本沒有更新你的循環中的選擇。 – StackFlowed 2015-03-31 15:42:32

+0

@StackFlowed:好吧,不僅如此 - 它不會編譯......它沒有明確分配。 – 2015-03-31 15:43:11

+0

除此之外,如果'choice'永遠不會改變,那麼循環內部的'switch'有什麼用處呢,它會一次又一次地執行相同的情況 – silentprogrammer 2015-03-31 15:44:55

回答

0
  1. 粗略地的頂部:初始化stop上聲明:

char stop = 'N';

  1. 更好:將while替換爲dowhile循環:

    做{

    }而(停止== 'N')

0

「停止」 是指在一個空白內存空間時,你是指它在第一次去通過你的while循環,所以它永遠不會啓動。您需要初始化它或重新安排循環結構。此外,似乎您需要提示用戶「選擇」,除非該部分代碼已被刪除。