Java While循环问题?

Java While循环问题?,java,while-loop,options,choice,Java,While Loop,Options,Choice,每次我从选项列表中键入选项时,它总是告诉我再次键入选项,只有当用户键入的不是选项时,它才应该这样做 对于yes和no选项,我在while循环中遇到了同样的问题,但是在修复之后,我看不出这与除了更多选项之外我仍然存在问题的区域之间有任何区别 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package testing.tests; import

每次我从选项列表中键入选项时,它总是告诉我再次键入选项,只有当用户键入的不是选项时,它才应该这样做

对于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;
            }
        }
**

**

本声明:

if ("population".equals(test_run) & "income".equals(test_run) &
"password".equals(test_run) & "randomize".equals(test_run)) {
            LOOP_2 = 1;
        }
永远不会执行。您应该使用or运算符
|
,而不是
&
。正如代码当前所示,要使此条件评估为真,测试运行必须等于所有选项,这是不可能的。

此语句:

if ("population".equals(test_run) & "income".equals(test_run) &
"password".equals(test_run) & "randomize".equals(test_run)) {
            LOOP_2 = 1;
        }
永远不会执行。您应该使用or运算符
|
,而不是
&
。正如代码当前所示,要使此条件计算为true,测试运行必须等于所有选项,这是不可能的。

更改此行

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


|
是逻辑OR运算符,这是您应该使用的

&
评估操作的两个方面,因此您的
if
将评估
测试运行
是否等于
“人口”
“收入”
“密码”
“随机”
,这是不可能的

编辑:

由于第一个
if
没有改变任何东西(
LOOP\u 2
将保留其值
0
),因此我建议整个
if
,换句话说,如果
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 ("population".equals(test_run) & "income".equals(test_run) & "password".equals(test_run)& "randomize".equals(test_run)) {
        LOOP_2 = 1;
    }


|
是逻辑OR运算符,这是您应该使用的

&
评估操作的两个方面,因此您的
if
将评估
测试运行
是否等于
“人口”
“收入”
“密码”
“随机”
,这是不可能的

编辑:

由于第一个
if
没有改变任何东西(
LOOP\u 2
将保留其值
0
),因此我建议整个
if
,换句话说,如果
if,删除第一个
if

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

现在是学习如何正确使用
调试器的好时机。在你的
循环中的不同点上触发一些断点,然后测试哪个变量引起了问题。是的,一个调试器。放置一些断点,如果您使用==来比较字符串,请将其替换为equals()。(但我在撰写此评论时还没有发现任何==带有字符串)现在是学习如何正确使用
调试器的好时机。在你的
循环中的不同点上触发一些断点,然后测试哪个变量引起了问题。是的,一个调试器。放置一些断点,如果您使用==来比较字符串,请将其替换为equals()。(但我在写这条评论的时候还没有发现任何带有字符串的==另外,考虑到这条语句测试的是与前面的if语句完全相反的if语句,您只需要使用“else”。@JaTochNietDan是的,您是对的,但是最好删除第一个
if
语句,因为它不会改变任何东西。此外,考虑到此语句与前面的if语句完全相反,您只需要使用“else”。@JaTochNietDan是的,您是对的,但是最好删除第一个
if
语句,因为它不会改变任何东西。