2013-01-20 47 views
1

好吧,這裏是代碼,第一部分是試圖繼續滾動2個骰子,直到他們添加到7或11,然後繼續其餘的代碼。我只能讓它滾動一次,如果它是7或11,則代碼停止。我不知道如何解決這個問題。第一部分是一個循環(需要成爲第一部分),我不知道如何關閉它

import java.io.*; 

public class JavaOlympics { 

    public static void main(String args[]) throws IOException { 
     int die1, die2, total; 
     // Continue loop until reaches a total of 7 or total of 11 
     while (true) { 
      die1 = 1 + (int) (Math.random() * 6); 
      die2 = 1 + (int) (Math.random() * 6); 
      // Display the dice roll 
      System.out.println("Die_1 = " + die1); 
      System.out.println("Die_2 = " + die2); 
      // Add the results 
      total = die1 + die2; 
      // Output total 
      System.out.println("Total = " + total); 
      System.out.println(); 
      // Break loop when total of 7 or 11 is reached 
      if (total == 7 || total == 11) 
       break; 

      String s1, s2; 
      // DataInputStream dis = new DataInputStream(System.in); //deprecated 
      BufferedReader dis = new BufferedReader(new InputStreamReader(System.in)); 
      // Ask for string input 
      System.out.println("Input two strings one by one"); 
      s1 = dis.readLine(); 
      s2 = dis.readLine(); 
      // Organize strings in alphabetical order 
      System.out.println("\nStrings in alphabetical order"); 
      if (s1.compareTo(s2) <= 0) { 
       System.out.println(s1); 
       System.out.println(s2); 
      } else { 
       System.out.println(s2); 
       System.out.println(s1); 
      } 
      // Count characters in the strings 
      System.out.println("\nNo.of characters"); 
      System.out.println(s1 + " :: " + s1.length()); 
      System.out.println(s2 + " :: " + s2.length()); 

      // Convert strings between lowercase and uppercase 
      System.out 
        .println("\nFirst String in Uppercase and Second String in Lowercase"); 
      System.out.println(s1.toUpperCase()); 
      System.out.println(s2.toLowerCase()); 

      String s; 
      // DataInputStream dis2 = new DataInputStream(System.in); // deprecated 
      BufferedReader dis2 = new BufferedReader(new InputStreamReader(System.in)); 
      System.out.println("Enter a sentence"); 
      // Request sentence input from user 
      s = dis2.readLine(); 
      // Count number of spaces 
      int space_count = 0; 
      for (int i = 0; i < s.length(); i++) { 
       if (s.charAt(i) == ' ') { 
        space_count++; 
       } 
      } 
      // Output number of words 
      System.out.println("Number of words = " + (space_count + 1)); 
      // Output number of characters and average characters per word 
      System.out.println("Number of characters = " + (s.length())); 
      System.out.println("Average number of characters/word = " 
        + (1.0 * (s.length()/space_count))); 
     } 
    } 
} 
+0

格式化您的代碼! – MrSmith42

+0

問題是額外的「}」,如果問題沒有問題...標記完成:-) – ggrandes

回答

0

然後繼續你的所有代碼都是封閉在while循環的代碼

的其餘部分。當你在其中出現break時,沒有更多的代碼需要執行。

如果您使用了合理的縮進,將會容易得多。

要解決,你可能只需要移動一個右大括號。

0

所有的代碼是while循環中,你已經錯過了關閉一個封閉的支架,你需要循環結束

+0

哇我現在真的很笨。謝謝! – Frisbee68

+0

如果一切都很好,現在請標記爲答案:) –

0

究竟你想在你的代碼做。你的代碼工作正常,但它運行到一個循環,除非和直到2個骰子的總和的值等於11或7.

你的整個代碼是在while循環中。所以從我的你的問題的瞭解,如果你希望你的代碼的其餘部分while循環完成後while循環如下

while(true) 
{ 
die1 = 1 + (int)(Math.random()*6); 
die2 = 1 + (int)(Math.random()*6); 
//Display the dice roll 
System.out.println("Die_1 = " + die1); 
System.out.println("Die_2 = " + die2); 
//Add the results 
total = die1 + die2; 
//Output total 
System.out.println("Total = " + total); 
System.out.println(); 
//Break loop when total of 7 or 11 is reached 
if (total == 7 || total == 11) break; 
} // close your while loop here and remove one brace at the end of your class. 

如果我的理解是錯誤的執行則正好接近你可以糾正我

相關問題