1

これがコードです。最初のセクションでは、7 または 11 になるまで 2 つのサイコロを転がし続け、残りのコードを続けます。私はそれを 1 回しか転がすことができず、それが 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)));
        }
    }
}
4

3 に答える 3

0

その後、残りのコードを続行します

すべてのコードはwhileループで囲まれています。それをbreak抜けると、実行するコードはもうありません。

賢明なインデントを使用したかどうかを確認する方がはるかに簡単です。

修正するには、おそらく閉じ中括弧を動かす必要があります。

于 2013-01-20T20:16:09.443 に答える
0

すべてのコードはwhileループ内にあり、ループを終了する必要がある閉じ括弧を見逃しています

于 2013-01-20T20:17:28.183 に答える
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.

私の理解が間違っている場合は、訂正してください

于 2013-01-20T20:50:05.543 に答える