0

当我运行这段代码时,这是一个包含许多不同选项的菜单。它由许多循环组成。其中一些我还没有做。我的抛硬币模拟器有问题。虽然我有一个 for 循环,但循环只循环一次,而不是直到用户选择“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");
        }
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 回答