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上用if语句结束一个结果?_Java_Loops_If Statement_Statements - Fatal编程技术网

如何在java上用if语句结束一个结果?

如何在java上用if语句结束一个结果?,java,loops,if-statement,statements,Java,Loops,If Statement,Statements,我怎样才能改变这一点,使我在最后只收到一条消息?现在,它给出了所有的信息,或者仅仅是它们是不同的。因为这不是一个循环,我知道我不能设置实际的中断,但是在这个问题上我可以采取什么措施呢?我必须重写它吗?感谢您的帮助。String output=“”,然后将所有的System.out.printlns替换为output=,并将字符串更改为适当的输出。此外,字符串赋值需要位于嵌套if之前 然后在最外面的如果还有,则执行System.out.println(输出) 将else条件与if一起使用。我不确定

我怎样才能改变这一点,使我在最后只收到一条消息?现在,它给出了所有的信息,或者仅仅是它们是不同的。因为这不是一个循环,我知道我不能设置实际的中断,但是在这个问题上我可以采取什么措施呢?我必须重写它吗?感谢您的帮助。

String output=“”
,然后将所有的
System.out.println
s替换为
output=
,并将字符串更改为适当的输出。此外,字符串赋值需要位于嵌套if之前

然后在最外面的
如果还有
,则执行
System.out.println(输出)


将else条件与if一起使用。

我不确定组、列和节之间的关系,但您可以先检查最具体的情况,这样做。这避免了嵌套条件的多个级别

if (number1 + 8 == number2 || number1 - 8 == number2) {

    if ("abc".equals("def")) {

        if (cod1 == cod2) {

            if (some1.equals(some2)) {

                System.out.println("They're in the same group");
            } else

                System.out.println("They are in the same column");
            ;
        } else

            System.out.println("They are in the same section");
    } else

        System.out.println("They are in the same subgroup");
}

else {
    System.out.println("They are different");
}

只有当所有列中的组都是唯一的时,它才会起作用——例如,检查两个生物体是否是同一物种,然后扩大范围检查属,然后科才有效,因为在不同的属中不会有相同的物种名称;但是,通过首先检查街道名称来测试住址是否相等是无效的,因为在不同的城市中会有同名的街道。

在适当的情况下使用
else
。这是不同的逻辑。当名称相同但cod不同时,将您的代码与OP的代码进行比较。如果组在所有列中都是唯一的,则这是有效的;如果组在不同的列中重复,则不会重复。我们对数据的了解还不够,无法说明具体情况。我在紧跟着守则的段落中提到了这一点。
String output = "";
if (number1 + 8 == number2 || number1 - 8 == number2){
    output = "They are in the same subgroup";
    if (name1.equals(name2)){
        output="They are in the same section";
        if (cod1 == cod2){
            output="They are in the same column";
            if (some1.equals(some2)){
                output="They're in the same group";
            }
        }
    }
}

else{
    output="They are different";
}

System.out.println(output);
if (number1 + 8 == number2 || number1 - 8 == number2) {

    if ("abc".equals("def")) {

        if (cod1 == cod2) {

            if (some1.equals(some2)) {

                System.out.println("They're in the same group");
            } else

                System.out.println("They are in the same column");
            ;
        } else

            System.out.println("They are in the same section");
    } else

        System.out.println("They are in the same subgroup");
}

else {
    System.out.println("They are different");
}
if (some1.equals(some2)) {
  System.out.println("They're in the same group");
} else if (cod1 == cod2) {
  System.out.println("They are in the same column");
} else if (name1.equals(name2)) {
  System.out.println("They are in the same section");
} else if (number1 + 8 == number2 || number1 - 8 == number2) {
  System.out.println("They are in the same subgroup");
} else {
  System.out.println("They are different");
}