Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java while循环如何仅显示循环中的用户选择,而不是显示所有用户选项_Java_Loops_Output_User Input - Fatal编程技术网

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

java while循环如何仅显示循环中的用户选择,而不是显示所有用户选项,java,loops,output,user-input,Java,Loops,Output,User Input,您好,我正在处理这个菜单循环,但我对Java和eclipse还不熟悉,我有几个问题。我的主要问题是,当用户输入他们的选择时,程序不仅输出用户的选择,还输出用户选择之前菜单中的所有选择 例如,如果用户输入g,程序将读取: you have selected to quit you have selected the coin toss simulator you have selected the grade estimator 有人知道我如何使它只输出用户的选择吗 此外,您是否建议我将

您好,我正在处理这个菜单循环,但我对Java和eclipse还不熟悉,我有几个问题。我的主要问题是,当用户输入他们的选择时,程序不仅输出用户的选择,还输出用户选择之前菜单中的所有选择

例如,如果用户输入
g
,程序将读取:

you have selected to quit 
you have selected the coin toss simulator 
you have selected the grade estimator
  • 有人知道我如何使它只输出用户的选择吗
此外,您是否建议我将此循环嵌套在每个选择下,并创建一个运行用户选择的新循环,例如,当用户选择
g
时,grade estimator

任何帮助都将不胜感激,我已经被困在这个问题上一段时间了

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

q
t
条件括号更新
if
,如下所示:

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

如果条件不包含括号,所有行都会因此执行

我不是最棒的,但我很无聊,所以我要试一试! 首先,我建议你去买一些好书或者做一场马拉松式的辅导!有很多很好的视频教程:)

你这里的主要问题是范围。。问题是你的牙套。请看以下链接

设想一种情况,范围在
if(SomeBool)

1考虑以下代码:

{
...
    if(SomeBool)
    {
        int x = 5;
        x += 4;
        System.out.println(x)
    }
...
}
2-结果会和我写的一样

 {
    ...
        if(SomeBool)
        {
            int x = 5;
            {
                x += 4;
            }
            System.out.println(x)
        }
    ...
 }  
{
...
    if(SomeBool)
    int x = 5;
    x += 4;
...
}  
3-但是如果我写了它就会崩溃

 {
    ...
        if(SomeBool)
        {
            int x = 5;
            {
                x += 4;
            }
            System.out.println(x)
        }
    ...
 }  
{
...
    if(SomeBool)
    int x = 5;
    x += 4;
...
}  
在第一种情况下,所有代码都在同一范围内,因此可以访问
x

如果两个
x+=4
位于新的作用域级别,但
x
包含在父作用域中,因此此新嵌套作用域可以访问它

如果三个x在不同范围内声明为
x+=4语句。如果您在
If(SomeBool)
中省略括号,它将只执行下面的语句

案例三相当于:

{
...
    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
...
}  
当我们完成与我们的
if(SomeBool)
关联的代码时,
x
将被删除,因为我们不再在该范围内,它也不再被引用

因此,请尝试使用适当的缩进和大括号来清理代码。然后重新发布,因为它当前相当于:

{        
    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;
            }
        }

    }
}

希望这能给我们带来一些启示

请用适当的缩进正确格式化代码。这对我们来说更容易阅读,甚至可能在过程中向您指出您的问题。一定要格式化你的if语句,比如
if(){}
Wow,这是一个括号盛会!