我不知道如何修复此代码。第一部分是循环(需要是第一部分),我不知道如何关闭它

时间:2013-01-20 20:12:46

标签: java

好的,所以这里是代码,第一部分试图继续滚动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)));
        }
    }
}

3 个答案:

答案 0 :(得分:0)

  

然后继续剩下的代码

所有代码都包含在while循环中。当你break出现时,就没有更多的代码可以执行了。

如果你使用合理的缩进会更容易。

要修复,你可能只需要移动一个右大括号。

答案 1 :(得分:0)

你的所有代码都在while循环中,你错过了一个封闭的括号,你需要循环结束

答案 2 :(得分: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.

如果我的理解错了,你可以纠正我