简体   繁体   English

java while循环如何在循环中仅显示用户选择,而不是所有用户选项

[英]java while loop how to display only the users selection within a loop and not all user options

Hello I am working on this menu loop, but I am new to Java and eclipse and I have a few questions. 您好,我正在此菜单循环上工作,但是我是Java和eclipse的新手,并且有几个问题。 My main issue is that when the user enters their selection, the program doesn't just output the user's selection, but also outputs all selections in the menu before the user's selection. 我的主要问题是,当用户输入他们的选择时,该程序不仅会输出用户的选择,还会在用户选择之前在菜单中输出所有选择。

For example if the user enters g the program will read: 例如,如果用户输入g则程序将读取:

you have selected to quit 
you have selected the coin toss simulator 
you have selected the grade estimator
  • Does anyone know how I can make it so it outputs only the user's selection? 有谁知道我该怎么做,以便仅输出用户的选择?

Also, would you recommend I nest this loop under each selection and create a new loop that runs the user's selcetion, for example grade estimator when they select g ? 此外,您是否建议我将此循环嵌套在每个选择下,并创建一个运行用户选择的新循环,例如当用户选择g时的成绩估算器?

Any help would be appreciated, I've been stuck on this for a while! 任何帮助将不胜感激,我已经坚持了一段时间!

{        
    Scanner anotherScanner = new Scanner(System.in);

    boolean usersSelection = false;

    String c = "againAgain";
    String upper = c.toUpperCase();
    while (!usersSelection)
    {
        System.out.println("" + "Selection: ");

        if (anotherScanner.hasNext("q"))
            c = anotherScanner.next();

        {
            usersSelection = true;

            System.out.println("you have selected to quit");
            ;
        }

        if (anotherScanner.hasNext("t"))
            c = anotherScanner.next();

        { 
            usersSelection = true;
            System.out.println("you have selected the coin toss estimator");
            ;
        }

        if (anotherScanner.hasNext("g"))
        {
            {
                c = anotherScanner.next();
                {
                    usersSelection = true;
                    System.out.println("you have selected the grade estimator");
                }
            }
        if (anotherScanner.hasNext("C"))
        {
             c = anotherScanner.next();

             {usersSelection = true;}

             System.out.print("You have selected color Challenge");
        }

        else
              break;

    }

    // { { System.out.print("Selection");}
}
}}}

Update if for q and t condition bracket like below : 更新ifqt条件托架象下面这样:

if (anotherScanner.hasNext("q"))
        {     
          c = anotherScanner.next();
          usersSelection = true;
          System.out.println("you have selected to quit");
        }

The if condition not containing bracket due to which all the lines get executed if条件不包含括号,则将执行所有行

I'm not the greatest, but I'm bored so I'll give this a shot! 我不是最伟大的人,但是我很无聊,所以我会试一试! First off I would suggest picking up some good books or doing a marathon of tutorials! 首先,我建议您挑选一些好书或参加一些马拉松式的教程! There are a lot of good video tutorials out there :) 有很多不错的视频教程:)

Your main problem here is scope.. ie) The problem is your braces. 您的主要问题是范围..即,问题是您的牙套。 Please look at the following links! 请查看以下链接!

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html http://mindprod.com/jgloss/scope.html http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html http://mindprod.com/jgloss/scope.html

Imagine a case where the scope is inside if(SomeBool) 想象一下范围在if(SomeBool)内部的if(SomeBool)

1 - consider the following code: 1-考虑以下代码:

{
...
    if(SomeBool)
    {
        int x = 5;
        x += 4;
        System.out.println(x)
    }
...
}

2 - It would have the same result as if I wrote 2-与我写的结果相同

 {
    ...
        if(SomeBool)
        {
            int x = 5;
            {
                x += 4;
            }
            System.out.println(x)
        }
    ...
 }  

3 - However it would crash if I wrote 3-但是如果我写的话会崩溃

{
...
    if(SomeBool)
    int x = 5;
    x += 4;
...
}  

In case one, all the code is in the same scope and thus can access x . 在第一种情况下,所有代码都在同一范围内,因此可以访问x

In case two x += 4; 如果两个x += 4; is in a new level of scope, but x is contained in parent scope, so it is accessible by this new nested scope. 处于新的作用域级别,但是x包含在父作用域中,因此此新的嵌套作用域可以访问它。

In case three x is declared within the different scope as the x += 4; 如果在不同范围内将三个x声明为x += 4; statement. 声明。 If you leave out brackets for an if(SomeBool) it will only act out the proceeding statement. 如果省略if(SomeBool)方括号, if(SomeBool)只会执行下面的语句。

Case three is equivalent to: 情况三等效于:

{
...
    if(SomeBool)
    { // Enter scope
        int x = 5; // x created
    } // Leave scope x deleted

    x += 4;  // There is no x here
    System.out.println(x) // No x here either
...
}  

When we finish the code associated with our if(SomeBool) , x will be deleted because we are no longer in that scope and it is no longer referenced. 当我们完成与if(SomeBool)相关联的代码时, x将被删除,因为我们不再在该范围内并且不再被引用。

So try cleaning up your code with proper indents and braces. 因此,请尝试使用适当的缩进和花括号来清理代码。 Then repost, because it is currently the equivalent of: 然后重新发布,因为它当前等效于:

{        
    Scanner anotherScanner = new Scanner(System.in);
    boolean usersSelection = false;

    String c = "againAgain";
    String upper = c.toUpperCase();
    while (!usersSelection)
    {
        System.out.println("" + "Selection: ");

        if (anotherScanner.hasNext("q"))
        {
            c = anotherScanner.next();
        }

        usersSelection = true; 
        System.out.println("you have selected to quit");

        if (anotherScanner.hasNext("t"))
        {
            c = anotherScanner.next();
        }

        usersSelection = true;
        System.out.println("you have selected the coin toss estimator");

        if (anotherScanner.hasNext("g"))
        {
            c = anotherScanner.next();
            usersSelection = true;
            System.out.println("you have selected the grade estimator");
            if (anotherScanner.hasNext("C"))
            {
                c = anotherScanner.next();
                usersSelection = true;
                System.out.print("You have selected color Challenge");
            }
            else
            {
                break;
            }
        }

    }
}

Hope this sheds some light! 希望这会有所启发!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM