2015-11-06 86 views
1

我試圖轉換同時while循環和程序甚至編譯,但沒有任何反應。 原始do-while循環代碼:嘗試轉換同時循環到while循環

import java.util.Scanner; 

public class CoinFlip 
{ 
    public static void main(String[] args) 
    { 
     Scanner keyboard = new Scanner(System.in); 

     String coin, again; 
     int flip, streak = 0; 

     do 
     { 
      flip = 1 + (int)(Math.random()*2); 

      if (flip == 1) 
       coin = "HEADS"; 
      else 
       coin = "TAILS"; 

      System.out.println("You flip a coin and it is... " + coin); 

      if (flip == 1) 
      { 
       streak++; 
       System.out.println("\tThat's " + streak + " in a row...."); 
       System.out.print("\tWould you like to flip again (y/n)? "); 
       again = keyboard.next(); 
      } 
      else 
      { 
       streak = 0; 
       again = "n"; 
      } 
     } while (again.equals("y")); 

     System.out.println("Final score: " + streak); 
    } 
} 

如何它看起來像後,我試圖做while循環從do-while循環:

import java.util.Scanner; 

public class CoinFlip2 
{ 
    public static void main(String[] args) 
    { 
     Scanner keyboard = new Scanner(System.in); 

     String coin, again; 
     int flip, streak = 0; 
     again = keyboard.next(); 
     //flip = 1 + (int) (Math.random()*2); 

     while (again.equals("y")) 
     { 
      flip = 1 + (int) (Math.random()*2); 
      if (flip == 1) 
      { 
       coin = "HEADS"; 
      } 
      else 
      { 
       coin = "TAILS"; 
      System.out.println("You flip a coin and it is..." + coin); 
      } 
      if (flip == 1) 
      { 
       streak++; 
       System.out.println("\tThat's " + streak + " in a row...."); 
       System.out.print("\tWould you like to flip again (y/n)? "); 
       //again = keyboard.next(); 
      } 
      else 
      { 
       streak = 0; 
       again = "n"; 
      } 
      System.out.println("Final score: " + streak); 
     } 
    } 
} 

代碼編譯沒有問題,但我看到後運行空白輸出它。

+2

DO-while循環保證至少運行一次,而循環都沒有。你得到一個空白的輸出,因爲你正在等待第11行的鍵盤輸入 –

+0

你不能。 'do-while'loops和'while'循環是不可互換的,至少沒有額外的布爾變量。你爲什麼要嘗試? – EJP

回答

2

那是因爲你從來沒有初始化again變量y,所以它永遠不匹配狀態,以進入while

public static void main(String[] args) { 
    Scanner keyboard = new Scanner(System.in); 

    String coin, again = "y"; 
    int flip, streak = 0; 
    // flip = 1 + (int) (Math.random()*2); 

    while (again.equals("y")) { 
     flip = 1 + (int) (Math.random() * 2); 
     if (flip == 1) { 
      coin = "HEADS"; 
     } else { 
      coin = "TAILS"; 
      System.out.println("You flip a coin and it is..." + coin); 
     } 
     if (flip == 1) { 
      streak++; 
      System.out.println("\tThat's " + streak + " in a row...."); 
      System.out.print("\tWould you like to flip again (y/n)? "); 
      again = keyboard.next(); 
      // again = keyboard.next(); 
     } else { 
      streak = 0; 
      again = "n"; 
     } 
    } 
    System.out.println("Final score: " + streak); 
    keyboard.close(); 
} 

OUTPUT:

That's 1 in a row.... 
Would you like to flip again (y/n)? y 
You flip a coin and it is...TAILS 
Final score: 0 
1

初始化您again"y"

String coin, again; 
int flip, streak = 0; 
again = "y"; 
... 

,並要求在while循環的鍵盤輸入:

if (flip == 1) 
{ 
    streak++; 
    System.out.println("\tThat's " + streak + " in a row...."); 
    System.out.print("\tWould you like to flip again (y/n)? "); 
    again = keyboard.next(); 
} 
+0

謝謝那塊岩石! 我理解正確,如果 again = keyboard.next(); 在這裏被評論,然後程序總是自動爲我輸入「y」? – Anonymous

+0

那麼它不會再''再次'所以你會遇到一個無限循環! – ParkerHalo