2016-01-06 174 views
-1

我已經創建了程序的基本結構,但無論我嘗試什麼,我都無法退出循環。打破我的while循環

另外我希望它顯示用戶將輸入的信息;從鍵盤到控制檯,一旦退出循環,但我不知道如何做到這一點。

import java.util.Scanner; 

public class Requirement1 { 

    public static void main(String[] args) { 

     Scanner scan = new Scanner (System.in); 
     String name, game, time, points; 
     int i = 0; 

     System.out.println("Please Enter Your Name"); 
     name = scan.nextLine(); 
     System.out.println("Players Name: " + name); 
     while (i < 100) 
     { 

     Scanner scan2 = new Scanner(System.in); 
     System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")"); 
     game = scan.nextLine(); 
     System.out.println("Game: " + game); 

     Scanner scan3 = new Scanner (System.in); 
     System.out.println("Please Enter Your Score"); 
     points = scan.nextLine(); 
     System.out.println("Your Score: " + points); 

     Scanner scan4= new Scanner (System.in); 
     System.out.println("Please Enter the amount of time Spent Playing in Minutes"); 
     time = scan.nextLine(); 
     System.out.println("Time played: " + time); 
     } 



    } 
+2

你試圖做什麼來擺脫循環?我沒有看到任何這樣的嘗試。 –

+6

你正在做(我<100),你在哪裏遞增我,以便它會增長每次循環運行?順便說一句我不認爲你希望你的while循環運行,直到我<100,你想要的東西是這樣的:while(scan.hasNextLine()) – krackmoe

+1

要添加到@ SotiriosDelimanolis的評論。當'(i <100)'爲假時,循環會退出,但是'i'在循環內部不會發生變化! –

回答

1

總的來說,我喜歡用for循環,當我知道的時候循環運行的確切人數。如果我不知道循環將運行多少次,那麼我喜歡使用while循環。

一個for循環:

import java.util.Scanner; 

public class Requirement1 { 

    public static void main(String[] args) { 

     Scanner scan = new Scanner (System.in); 
     String name, game, time, points; 

     System.out.println("Please Enter Your Name"); 
     name = scan.nextLine(); 
     System.out.println("Players Name: " + name); 

     for (int i = 0; i < 100; i++) { 

      Scanner scan2 = new Scanner(System.in); 
      System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")"); 
      game = scan.nextLine(); 

      if (game.equals("quit")) { 
       break; 
      } 
      System.out.println("Game: " + game); 

      Scanner scan3 = new Scanner (System.in); 
      System.out.println("Please Enter Your Score"); 
      points = scan.nextLine(); 
      System.out.println("Your Score: " + points); 

      Scanner scan4= new Scanner (System.in); 
      System.out.println("Please Enter the amount of time Spent Playing in Minutes"); 
      time = scan.nextLine(); 
      System.out.println("Time played: " + time); 
     } 
    } 

一個while循環:

import java.util.Scanner; 

public class Requirement1 { 

    public static void main(String[] args) { 

     Scanner scan = new Scanner (System.in); 
     String name, game, time, points; 

     System.out.println("Please Enter Your Name"); 
     name = scan.nextLine(); 
     System.out.println("Players Name: " + name); 

     while (true) { 

      Scanner scan2 = new Scanner(System.in); 
      System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")"); 
      game = scan.nextLine(); 

      if (game.equals("quit")) { 
       break; 
      } 
      System.out.println("Game: " + game); 

      Scanner scan3 = new Scanner (System.in); 
      System.out.println("Please Enter Your Score"); 
      points = scan.nextLine(); 
      System.out.println("Your Score: " + points); 

      Scanner scan4= new Scanner (System.in); 
      System.out.println("Please Enter the amount of time Spent Playing in Minutes"); 
      time = scan.nextLine(); 
      System.out.println("Time played: " + time); 
     } 
    } 
+0

謝謝。這非常有幫助! –

+0

我已經面臨另一個問題了,是否可以通過電子郵件向您發送更多幫助? –

+0

@YakubMiah - 在stackoverflow上問另一個問題可能會更好,這樣可以讓更多人看到它(也許給你一個比我更好的答案)。 –

5

在循環的內部包含i++

4

爲了打破while循環,你需要要麼使i < 100假,或添加一個明確的聲明,以停止循環(breakreturnthrowSystem.exit,根據您的要求)。

如果希望當用戶輸入"quit"打破循環,並執行代碼迴路如下:

System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")"); 
game = scan.nextLine(); 
if (game.equals("quit")) { 
    break; 
} 

目前,i不會從0變化,所以i < 100始終是真實的。另外,它不會被讀取。除非你需要它的一些其他的目的,你可以刪除i和更改while循環聲明:

while (true) { 
    // ... 
} 

這種情況顯然不可能是假的,所以你需要在循環的明確突破。

1

另一種方法是利用for循環的,並且是更正確的爲您的需求:

for (int i = 0; i < 100; i++) { 
    //your code 
} 
0

你的循環是一個簡單的計數器,你可能想用一個for循環,因爲它來代替它表示更好的環路不會因爲一些操作循環內發生的突破:

for(int i = 0 ; i < 100 ; i++) { 
    // block running 100 times 
} 
0

請儘量避免使用做/而贊成的for循環。如果遊戲循環應該在用戶鍵入退出時結束,請添加一個break語句。

for (i = 0; i < 100; i++) { 

    // get user's game choice from console 

    if (game.equals("quit")){ 
     break; 
    } 

    // if they didn't type quit ... 
}