简体   繁体   English

如何在Java中中断'if'语句?

[英]How to break 'if' statement in java.?

In the following code. 在下面的代码中。 I want to execute only first if statement. 我只想先执行if语句。 While executing it is displaying output, Valid try again. 执行时显示输出,请重试有效。 I want it, to print only 'valid' NOT to display 'try again'. 我想要它,仅打印“有效”而不显示“重试”。 How to do in java.?? 在Java中怎么做? I am a beginner. 我是初学者。

public class Test {
    public void validate(int age) {
        if (age >= 18) {
            System.out.println("Valid.");
        }
        if (age < 18) {
            System.out.println("Not valid.");
        } else {
            System.out.println("try again.");
        }
    }

    public static void main(String[] args) {
        Test t = new Test();
        t.validate(18);
    }
}

In short: 简而言之:

public void validate(int age) {
    if (age >= 18) {
        System.out.println("Valid.");
    } else {
        System.out.println("Not valid.");
        System.out.println("Try again.");
    }
}

Explanation: The first if only prints a text if the age is at least 18, but that has nothing to do with the following lines. 说明:第一if只打印文本如果年龄为至少18个,但无关与以下行。 Those act on their own and have two cases: either the user is below 18 or the "try again" text is displayed. 这些行为独立发生,并有两种情况:用户未满18岁或显示“重试”文本。

What you want to do is to check "is the age at least 18?" 您要做的是检查“年龄至少18岁?” (as you already do) and then display either "valid" or "invalid". (如您已经做的那样),然后显示“有效”或“无效”。 Do this by adding the else case to this check, and you are fine. 通过将else大小写添加到此检查中来执行此操作,就可以了。

EDIT: I appended the second line to the else case due to the second comment that I understand as "I need the second message to be displayed but only in case the age is invalid". 编辑:由于我理解为“我需要显示第二条消息,但仅在年龄无效的情况下”,因此第二条评论将第二行添加到else情况。

    public void validate(int age) {
        if (age >= 18) {
            System.out.println("Valid.");
        } else if (age < 18) {
            System.out.println("Not valid.");
        } else {
            System.out.println("try again.");
        }
    }

    public static void main(String[] args) {
        Test t = new Test();
        t.validate(18);
    }

Problem with your code was even if age is >=18 it will still check if it is also <18, so it will always say try again when age is >=18. 与您的代码的问题是即使age为> = 18它仍然会检查它是否也<18,所以它总是会说try againage为> = 18。 Also, please check in comments that it will never say try again with this corrected code because under no circumstances will age be not <18 and >=18 at the same time 另外,请签入注释,使其永远不会说try again此更正后的代码,因为在任何情况下age都不会同时小于<18和> = 18

EDIT I like Dennis' code better because I think this is what you are trying to achieve. 编辑我更喜欢Dennis的代码,因为我认为这是您要实现的目标。

First of all, lets get some facts about integers sorted out. 首先,让我们得到一些有关整理出来的整数的事实。

  1. If an integer is >= 18, that means it is not < 18 如果整数> = 18,则表示它不是<18
  2. If an integer is < 18, that means it is not >= 18 如果整数<18,则表示不是> = 18
  3. Any a number is an integer, it is either >= 18 or < 18. It cannot be both. 任何数字都是整数,它是> = 18或<18。不能两者都为。 It cannot be neither. 不能两者都不是。

Now to your code. 现在到您的代码。 I could write your method like this: 我可以这样写你的方法:

public void validate(int age) {
    if (age >= 18) {
        System.out.println("Valid.");
    }
    if (age < 18) {
        System.out.println("Not valid.");
    }
}

or more simply (relying on the above facts) 或更简单(基于上述事实)

public void validate(int age) {
    if (age >= 18) {
        System.out.println("Valid.");
    } else {
        System.out.println("Not valid.");
    }
}

That will write either Valid or Not Valid . 这将写入ValidNot Valid

I could also write the code like this: 我也可以这样写代码:

   public void validate(int age) {
        if (age >= 18) {
            System.out.println("Valid.");
            return;
        }
        if (age < 18) {
            System.out.println("Not valid.");
        } else {
            System.out.println("try again.");
        }
    }

but the else part is redundant because of the above mentioned facts about numbers. 但是else部分是多余的,因为上述有关数字的事实。 Notice how I used a return to "break" the control flow. 注意我如何使用return来“破坏”控制流。

You can't use a break statement in this context. 您不能在这种情况下使用break语句。 The Java Language Specification states in JLS 14.15 JLS 14.15中的Java语言规范规定

For an unlabelled break statement: 对于未标记的break语句:

"If no switch, while, do, or for statement in the immediately enclosing method, constructor, or initializer contains the break statement, a compile-time error occurs." “如果立即封闭的方法,构造函数或初始化程序中没有switch,while,do或for语句包含break语句,则会发生编译时错误。”

We don't have switch , while , do or for statement here. 我们这里没有switchwhiledofor语句。

For a labelled break, the JLS says. 对于带标签的休息,JLS说。

" A break statement must refer to a label within the immediately enclosing method, constructor, initializer, or lambda body. There are no non-local jumps. If no labeled statement with Identifier as its label in the immediately enclosing method, constructor, initializer, or lambda body contains the break statement, a compile-time error occurs. " break语句必须引用立即封闭的方法,构造函数,初始值设定项或lambda主体内的标签。没有非本地跳转。如果在立即封闭的方法,构造函数,初始值设定项中没有以Identifier为标签的标记语句,或lambda主体包含break语句,则会发生编译时错误。

... so I suppose it would be possible to write this: ...所以我想可以这样写:

   public void validate(int age) {
       label: { 
           if (age >= 18) {
               System.out.println("Valid.");
               break label;
           }
           if (age < 18) {
               System.out.println("Not valid.");
           } else {
               System.out.println("try again.");
           }
       }
   }

But frankly, that is horrible. 但坦率地说,这太可怕了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM