0

このコードを実行すると、さまざまなオプションを持つメニューが表示されます。多くのループで構成されています。まだ作っていないものもあります。コイントスシミュレーターに問題があります。forループがありますが、ループはユーザーが「0」を選択してプログラムを終了し、メインメニューに戻るまでではなく、1回だけ循環しますが、他の問題は、シミュレーターがメインメニューに戻らないことです。選択されました。何が起こっている???これが私のトラブルの例です。

シード値を入力してください: 3

===== CS302 ツールボックス =====

T > コイントスシミュレーター

G > グレードエスティメーター

C > カラーチャレンジ

Q > QUIT 選択したコード文字を入力してください: t

コイントスシミュレーター

0 を入力して終了します。何回投げますか?4

3.0 表と 1.0 裏は、75.0% が表であることを意味します

その後、シミュレーターは「Enter 0 to quit. How many tosses?」と尋ねません。また。実際、別の数字、たとえば 3 を押すと、これが表示されます。また、ゼロはシミュレーターを終了せず、メインメニューを再度表示します。

3 は有効な選択ではありません。

===== CS302 ツールボックス =====

T > コイントスシミュレーター

G > グレードエスティメーター

C > カラーチャレンジ

Q > やめる

    System.out.println("");
    System.out.println( "===== CS302 TOOL BOX =====\nT > COIN TOSS SIMULATOR\nG > GRADE ESTIMATOR\nC > COLOR CHALLENGE\nQ > QUIT");

    {        
    Scanner anotherScanner = new Scanner(System.in);
    boolean usersSelection = false;
    String c;
    while (!usersSelection)

        {
        {
        System.out.print(""+ "Type code letter for your choice: ");
        }





        if (anotherScanner.hasNext("q|Q"))
            {
            c = anotherScanner.next();
            usersSelection = true;

            System.out.println(""
            + ""
            + "Good-Bye");
            break;
            }






    if (anotherScanner.hasNext("t|T")){

        c = anotherScanner.next();
        usersSelection = true;
        System.out.println("");
        System.out.println("COIN TOSS SIMULATOR");
        System.out.println("");
        System.out.println("Enter 0 to quit. How many tosses?");

        Random rand = new Random();

        Scanner insideScanner = new Scanner(System.in);
        int feeble = insideScanner.nextInt();
        double heads = 0;
        double tails = 0;
        boolean headsvsTails;
        headsvsTails = rand.nextBoolean();

    for (int i =0; i < feeble; i++)
         {
         headsvsTails = rand.nextBoolean();

        if (headsvsTails == true){ heads++;}

        else {tails++;}

        } 
        System.out.println(heads + " heads and " + tails + " tails means " + (heads/(heads+tails)*100 + "% were heads"));

        }












    if  (anotherScanner.hasNext("g|G")) 
        {
        c = anotherScanner.next();
        usersSelection = true;
        System.out.println("");
        System.out.println("GRADE ESTIMATOR");
        Scanner gradeE = new Scanner(System.in);
        double exam1;
        double possiblePts;
        double programPts;
        double programPossible;
        double avg;
        double avge;
        double avrg;




    System.out.print("Enter exam points earned (int): ");
    exam1=gradeE.nextDouble();

    if (exam1<0)
                 {System.out.print("Enter exam points earned (int): ");
         exam1=gradeE.nextDouble();}

    System.out.print("Enter exam points possible (int): ");
    possiblePts=gradeE.nextDouble();

    if (possiblePts<0) {System.out.print("Enter exam points possible (int): ");
        possiblePts=gradeE.nextDouble();}

    System.out.print("Enter program points earned (int): ");
    programPts=gradeE.nextDouble();


            if (programPts<0) {System.out.print("Enter program points earned (int): ");
        programPts=gradeE.nextDouble();}

    System.out.print("Enter program points possible (int): ");
    programPossible=gradeE.nextDouble();

            if (programPossible<0) {System.out.print("Enter program points possible (int): ");
            programPossible=gradeE.nextDouble();}


        avge = exam1+programPts;
        avrg = possiblePts+programPossible;
        avg = avge/avrg*100;

        if (avg<60)
            System.out.println("Grade estimate for " +avg+ "% is a F");
        else if (avg<70)
            System.out.println("Grade estimate for " +avg+ "% is a D");
        else if (avg<80)
            System.out.println("Grade estimate for " +avg+ "% is a C");
        else if (avg<90)
            System.out.println("Grade estimate for " +avg+ "% is a B");
        else if (avg>=90)
            System.out.println("Grade estimate for " +avg+ "% is a A");
        }
4

1 に答える 1

1

あなたのコードには多くの間違いがあります。常に新しいスキャナーを宣言します。間隔に一貫性がなく、コードの一部が太平洋に広がる空白の海によって互いに分離されています。

ここに修正があります:

if (anotherScanner.hasNext("t|T")){

    c = anotherScanner.next();
    usersSelection = true;
    System.out.println("");
    System.out.println("COIN TOSS SIMULATOR");
    System.out.println("");
    System.out.println("Enter 0 to quit. How many tosses?");

    Random rand = new Random();

    Scanner insideScanner = new Scanner(System.in);
    int feeble = insideScanner.nextInt();
    double heads = 0;
    double tails = 0;
    boolean headsvsTails;
    headsvsTails = rand.nextBoolean(); // This is Worthless

    while ( feeble != 0 ) { //Pay attention to this while loop

        for (int i =0; i < feeble; i++) {
            headsvsTails = rand.nextBoolean();

            if (headsvsTails == true){ heads++;}

            else {tails++;}

        }
    System.out.println(heads + " heads and " + tails + " tails means " + (heads/(heads+tails)*100 + "% were heads"));
    System.out.println("Enter 0 to quit. How many tosses?"); //I ask the question again
    heads = 0;
    tails = 0;
    feeble = insideScanner.nextInt();//I get new input
    }
}

ここで重要なのは while ループです。私はあなたの弱い変数をその条件として使用します。ユーザーが 0 を指定しない限り実行されます。

友人や教授にあなたのコードを説明する時間をとってください。彼らに説明し、質問するよう促します。

また、これを読むことは必須だと思います。がっかりしないでください。3年前に書いたコードを見ると、よく似ている。あなたは良くなるでしょう。

*コードを修正したため、コメントからの入力に基づいて表と裏が加算されません。

于 2014-02-19T04:45:10.153 に答える