【问题标题】:java while loop how to display only the users selection within a loop and not all user optionsjava while循环如何仅显示循环内的用户选择而不是所有用户选项
【发布时间】:2014-02-11 18:22:52
【问题描述】:

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

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

you have selected to quit 
you have selected the coin toss simulator 
you have selected the grade estimator
  • 有谁知道我怎样才能让它只输出用户的选择?

另外,您是否建议我在每个选择下嵌套此循环并创建一个运行用户选择的新循环,例如当他们选择g 时的成绩估算器?

任何帮助将不胜感激,我已经坚持了一段时间!

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

【问题讨论】:

  • 请使用适当的缩进正确格式化您的代码。这对我们来说更容易阅读,甚至可以在此过程中向您指出您的问题。请务必格式化您的 if 语句,例如 if () {}
  • 哇,这是一个支架盛宴!

标签: java loops output user-input


【解决方案1】:

if 更新为qt 条件括号,如下所示:

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

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

【讨论】:

    【解决方案2】:

    我不是最棒的,但我很无聊,所以我会试一试! 首先,我会建议拿起一些好书或做一个马拉松式的教程!那里有很多很好的视频教程:)

    您的主要问题是范围。即)问题是您的大括号。请看以下链接!

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

    想象一下作用域在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)
            }
        ...
     }  
    

    3 - 但是如果我写它会崩溃

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

    第一种情况,所有代码都在同一个范围内,因此可以访问x

    如果两个x += 4; 在新级别的范围内,但x 包含在父范围内,因此可以通过这个新的嵌套范围访问。

    如果在 x += 4; 语句的不同范围内声明了三个 x。如果您为 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;
                }
            }
    
        }
    }
    

    希望这能有所启发!

    【讨论】:

      猜你喜欢
      • 2018-09-02
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 2018-10-04
      • 2013-07-05
      • 1970-01-01
      • 1970-01-01
      • 2017-01-07
      相关资源
      最近更新 更多