【问题标题】:While Loop Problems?While循环问题?
【发布时间】:2014-01-19 02:41:08
【问题描述】:

每次我从选项列表中输入一个选项时,它只会告诉我再次输入一个选项,只有在用户输入不是选项的内容时才应该这样做。

对于 yes 和 no 选项,我在 while 循环中遇到了同样的问题,但在修复之后,我看不出它与除了更多选项之外我仍然遇到问题的区域之间有任何区别。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testing.tests;

import java.util.Scanner;

/**
 *
 * @author andyoppenheimer
 */
public class TestingTests {

    static Scanner sc = new Scanner(System.in);

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here    

        String start_end;
        String test_run = null;
        int name_num = 0;
        int LOOP_1 = 0;
        int LOOP_2 = 0;

        while (LOOP_1 == 0) {
            System.out.println("Do you want to continue, yes or no?");
            System.out.print("----->: ");
            start_end = sc.next();
            if ("yes".equals(start_end)) {
                LOOP_1 = 1;
            }
            if ("no".equals(start_end)) {
                LOOP_1 = 1;
                System.exit(0);
            }
            if (!"no".equals(start_end) & !"yes".equals(start_end)) {
                LOOP_1 = 0;
            }
        }

**

while (LOOP_2 == 0) {
            System.out.println("Please pick your test to run.");
            System.out.println("population");
            System.out.println("income");
            System.out.println("password");
            System.out.println("randomize");
            System.out.print("----->: ");
            test_run = sc.next();
            if (!"population".equals(test_run) && !"income".equals(test_run) && !"password".equals(test_run) && !"randomize".equals(test_run)) {
                LOOP_2 = 0;
            }
            if ("population".equals(test_run) & "income".equals(test_run) & "password".equals(test_run) & "randomize".equals(test_run)) {
                LOOP_2 = 1;
            }

**

        }

        if ("population".equals(test_run)) {
        }

        if ("income".equals(test_run)) {
        }

        if ("password".equals(test_run)) {
        }

        if ("randomize".equals(test_run)) {
            System.out.println("Enter the number of names that will be randomized.");
            System.out.print("----->: ");
            name_num = sc.nextInt();
        }

    }
}

【问题讨论】:

  • 现在是学习如何正确使用debugger 的好时机。在 loops 的不同点启动一些断点并测试以查看导致问题的变量。
  • 是的,一个调试器。放置一些断点,如果你使用 == 来比较字符串,用 equals() 替换它。 (但在撰写此评论时,我还没有找到任何带字符串的 ==)

标签: java while-loop options choice


【解决方案1】:

改变这一行

if ("population".equals(test_run) & "income".equals(test_run) & "password".equals(test_run)& "randomize".equals(test_run)) {
        LOOP_2 = 1;
    }

到

if ("population".equals(test_run) || "income".equals(test_run) || "password".equals(test_run)
            || "randomize".equals(test_run)) {
    LOOP_2 = 1;
}

|| 是逻辑 OR 运算符,您应该使用它。

& 评估操作的双方,因此您的if 将评估test_run 等于"population"、"income"、"password" 和"randomize",这是不可能的。

编辑:

由于第一个if 没有改变任何东西(LOOP_2 将保留其值0),我将省略整个if,换句话说,删除第一个if

// Delete this
if (!"population".equals(test_run) && !"income".equals(test_run) && !"password".equals(test_run) && !"randomize".equals(test_run)) {
    LOOP_2 = 0;
}

【讨论】:

  • 另外,考虑到这个语句正在测试与前面的 if 语句完全相反的语句,你只需要使用 "else"。
  • @JaTochNietDan 是的,你是对的,但最好删除第一个 if 语句,因为它不会改变任何东西。
【解决方案2】:

此声明:

if ("population".equals(test_run) & "income".equals(test_run) &
"password".equals(test_run) & "randomize".equals(test_run)) {
            LOOP_2 = 1;
        }

永远不会执行。您应该使用或运算符||,而不是&。就目前的代码而言,要使此条件评估为真,test_run 必须等于所有选项,这是不可能的。

【讨论】:

    猜你喜欢
    • 2011-06-19
    • 2011-03-04
    • 1970-01-01
    • 2018-11-05
    • 2013-12-10
    • 2014-05-22
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多