簡體   English   中英

如何根據用戶輸入使這些循環繼續? 在Java Eclipse中

[英]how can I make these loops continue based on users input? In java eclipse

當我運行此代碼時,這是一個包含許多不同選項的菜單。 它包含許多循環。 其中一些我還沒有做。 我的拋硬幣模擬器遇到問題。 盡管我有一個for循環,但循環只循環一次,而不是直到用戶選擇“ 0”退出程序並返回主菜單為止,但是我的另一個問題是,如果0為0,模擬器將不會返回主菜單選擇。 到底是怎么回事??? 這是我遇到麻煩的一個例子。

輸入種子值:3

===== CS302工具箱=====

T>投幣模擬器

G>等級估算器

C>色彩挑戰

Q> QUIT鍵入代碼字母供您選擇:t

投幣模擬器

輸入0退出。 扔多少次? 4

3.0的正面和1.0的反面意味着75.0%是正面

然后模擬器不會要求“輸入0退出。要扔多少次?” 再次。 事實上,如果我按另一個數字,說3我會得到這個。 同樣,零不會結束模擬器並再次提示主菜單。

3不是有效的選擇。

===== CS302工具箱=====

T>投幣模擬器

G>等級估算器

C>色彩挑戰

問>退出

    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");
        }

好的,伙計,您的代碼有很多錯誤。 您不斷聲明新的掃描儀。 間距是不一致的,並且您的代碼的各部分之間由太平洋跨度的空白海洋相互分隔。

解決方法:

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,它將一直運行。

請花一些時間陪伴朋友或教授講解您的代碼。 向他們解釋,鼓勵他們提出問題。

我也認為閱讀本文是強制性的。 不要氣our。 當我查看三年前編寫的代碼時,它看起來很像這樣。 你會好起來的。

*固定代碼,因此根據注釋的輸入,正面/反面不會加法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM