'if'语句可以有多个'then'吗?

时间:2023-02-03 12:21:21

I do apologize if this question is too simple but I have tried Google and I cannot find a concrete answer (mostly because I do not know how to word my question).

如果这个问题太简单,我会道歉,但我已经尝试了谷歌,我找不到具体的答案(主要是因为我不知道如何说出我的问题)。

I am just wondering if an if statement in Java can have more than one 'then' i.e.

我只是想知道Java中的if语句是否可以有多个'then'即

public class test {

     if (condition) {
      then...;
      then 2...?;
     }
}

For example, the first condition is true. The first then occurs, which say throws an exception. Could I add on another then after that which would also occur after the exception is thrown?

例如,第一个条件是真的。第一个然后发生,这会引发异常。我可以添加另一个然后在抛出异常后也会发生吗?

Thanks for looking

谢谢你的期待

EDIT: I understand my question is a bit ambiguous. I guess what I'm trying to say is something like this:

编辑:我理解我的问题有点含糊不清。我想我想说的是这样的:

public class test {

  public String checkAnswer(String answer) throws Exception {
     if (!a == b) {
      throw new Exception;
      this.ans = this.ans + a;
     }
     else {
        return "You are wrong."
}

Is that second line (this.ans ...) allowed to be there? As in would both throw new Exception and that line both occur if the condition was not met?

是第二行(this.ans ......)允许存在吗?因为如果不符合条件,两者都会抛出新的异常和那条线?

5 个解决方案

#1


3  

Yes you can nest if blocks, if that is what you're asking. This is legal Java:

是的,你可以嵌套if block,如果这就是你所要求的。这是合法的Java:

if (foo) {
  if (bar) {
    System.out.println("both foo and bar are true");
  } else {
    System.out.println("foo is true, but bar is false");
  }
} else {
  if (bar) {
    System.out.println("foo is false, but bar is true");
  } else {
    System.out.println("both foo and bar are false");
  }
}

But this can also be written in a non-nested fashion:

但这也可以用非嵌套方式编写:

if (foo && bar) {
  System.out.println("both foo and bar are true");
} else if (foo && !bar) {
    System.out.println("foo is true, but bar is false");
} else if (!foo && bar) {
    System.out.println("foo is false, but bar is true");
} else {
    System.out.println("foo and bar are both false");
}

Edit
Regarding your new code:

编辑关于您的新代码:

public class test {

  public String checkAnswer(String answer) throws Exception {
     if (!a == b) {
      throw new Exception;
      this.ans = this.ans + a;
     }
     else {
        return "You are wrong."
}

First of all, it won't compile, and you should really strive to only post code that either compiles or that if your problem is a compilation issue, then is your best effort to get it to compile, and then you show your compilation errors.

首先,它不会编译,你应该真正努力只发布编译的代码,或者如果你的问题是编译问题,那么你最好努力让它编译,然后你展示你的编译错误。

Assuming you add parenthesis to new Exception(); your compiler should still show a problem since the code below the thrown exception is unreachable.

假设你为新的Exception()添加括号;您的编译器仍应显示问题,因为抛出异常下面的代码无法访问。

Instead, you will want the line under the throw exception to be above it. You will also want to correct your inequality statement:

相反,您将希望throw异常下的行位于其上方。您还需要更正不等式声明:

     if (a != b) {  // ** note the different equality test
       this.ans = this.ans + a;
       throw new AnswerWrongException("some string here");
     }

You have an amazing laboratory available to you -- your Java compiler and JVM, and you should test this stuff out and see what it tells you, as it can give you a correct and clear answer before any of us can.

你有一个令人惊奇的实验室 - 你的Java编译器和JVM,你应该测试这些东西并看看它告诉你什么,因为它可以在我们任何人之前给你一个正确而明确的答案。

#2


1  

First, no there are no multi-then(s) whatever that is. Given this question though,

首先,没有多个那么多。考虑到这个问题,

For example, the first condition is true. The first then occurs, which say throws an exception. Could I add on another then after that which would also occur after the exception is thrown?

例如,第一个条件是真的。第一个然后发生,这会引发异常。我可以添加另一个然后在抛出异常后也会发生吗?

You really are asking for a finally block, so use one.

你真的要求一个finally块,所以使用一个。

try {
  if (condition) {
    System.out.println("condition is true");
  } else {
    System.out.println("condition is false");
  }
} finally {
  System.out.println("This always prints");
}

Edit

if (a + b == c) is true, can I then put two different actions in the proceeding block (a = a + b) and (b = b + c)

如果(a + b == c)为真,我可以在前一个程序段中放置两个不同的动作(a = a + b)和(b = b + c)

Yes. And you can use += since you want to assign the result of the arithmetic to the first term -

是。你可以使用+ =,因为你想将算术的结果分配给第一个术语 -

int a = 1;
int b = 2;
int c = 3;
if (a + b == c) {
  a += b;
  b += c;
}
System.out.printf("a=%d, b=%d, c=%d%n", a, b, c);

#3


0  

No, an if statement can only have one then. If blah then blah. I'm not 100% positive, thought it probably also depends on the coding language. Though in java, no.

不,if语句只能有一个。如果等等等等等等等等。我不是100%肯定,认为它可能还取决于编码语言。虽然在java中,没有。

#4


0  

Everything within the if-block gets executed:

if块中的所有内容都会被执行:

public class test {

    if (condition) {
        methodA();
        methodB();
    }
}

Here, because both methods are in between the curly brackets ({}), both methodA() and methodB() get executed.

这里,因为两个方法都在大括号({})之间,所以方法A()和方法B()都被执行。

Introducing exception handling changes the answer depending on how you write the exception. If you write it so that it breaks out of the current code block at methodA(), then methodB() will not be executed. Of course, you would have to be purposely writing it this way.

引入异常处理会根据您编写异常的方式更改答案。如果你编写它以便它在methodA()的当前代码块中突破,那么将不会执行methodB()。当然,你必须故意以这种方式写作。

#5


0  

This has nothing to do with if statements.
What you want is a try-catch block:

这与if语句无关。你想要的是一个try-catch块:

 if (condition) {
     try {
         // some code that may throw exceptions
     } catch (SomeException e) {
         //
     }
     // some more code, which can also be in anoth try-catch
 }

#1


3  

Yes you can nest if blocks, if that is what you're asking. This is legal Java:

是的,你可以嵌套if block,如果这就是你所要求的。这是合法的Java:

if (foo) {
  if (bar) {
    System.out.println("both foo and bar are true");
  } else {
    System.out.println("foo is true, but bar is false");
  }
} else {
  if (bar) {
    System.out.println("foo is false, but bar is true");
  } else {
    System.out.println("both foo and bar are false");
  }
}

But this can also be written in a non-nested fashion:

但这也可以用非嵌套方式编写:

if (foo && bar) {
  System.out.println("both foo and bar are true");
} else if (foo && !bar) {
    System.out.println("foo is true, but bar is false");
} else if (!foo && bar) {
    System.out.println("foo is false, but bar is true");
} else {
    System.out.println("foo and bar are both false");
}

Edit
Regarding your new code:

编辑关于您的新代码:

public class test {

  public String checkAnswer(String answer) throws Exception {
     if (!a == b) {
      throw new Exception;
      this.ans = this.ans + a;
     }
     else {
        return "You are wrong."
}

First of all, it won't compile, and you should really strive to only post code that either compiles or that if your problem is a compilation issue, then is your best effort to get it to compile, and then you show your compilation errors.

首先,它不会编译,你应该真正努力只发布编译的代码,或者如果你的问题是编译问题,那么你最好努力让它编译,然后你展示你的编译错误。

Assuming you add parenthesis to new Exception(); your compiler should still show a problem since the code below the thrown exception is unreachable.

假设你为新的Exception()添加括号;您的编译器仍应显示问题,因为抛出异常下面的代码无法访问。

Instead, you will want the line under the throw exception to be above it. You will also want to correct your inequality statement:

相反,您将希望throw异常下的行位于其上方。您还需要更正不等式声明:

     if (a != b) {  // ** note the different equality test
       this.ans = this.ans + a;
       throw new AnswerWrongException("some string here");
     }

You have an amazing laboratory available to you -- your Java compiler and JVM, and you should test this stuff out and see what it tells you, as it can give you a correct and clear answer before any of us can.

你有一个令人惊奇的实验室 - 你的Java编译器和JVM,你应该测试这些东西并看看它告诉你什么,因为它可以在我们任何人之前给你一个正确而明确的答案。

#2


1  

First, no there are no multi-then(s) whatever that is. Given this question though,

首先,没有多个那么多。考虑到这个问题,

For example, the first condition is true. The first then occurs, which say throws an exception. Could I add on another then after that which would also occur after the exception is thrown?

例如,第一个条件是真的。第一个然后发生,这会引发异常。我可以添加另一个然后在抛出异常后也会发生吗?

You really are asking for a finally block, so use one.

你真的要求一个finally块,所以使用一个。

try {
  if (condition) {
    System.out.println("condition is true");
  } else {
    System.out.println("condition is false");
  }
} finally {
  System.out.println("This always prints");
}

Edit

if (a + b == c) is true, can I then put two different actions in the proceeding block (a = a + b) and (b = b + c)

如果(a + b == c)为真,我可以在前一个程序段中放置两个不同的动作(a = a + b)和(b = b + c)

Yes. And you can use += since you want to assign the result of the arithmetic to the first term -

是。你可以使用+ =,因为你想将算术的结果分配给第一个术语 -

int a = 1;
int b = 2;
int c = 3;
if (a + b == c) {
  a += b;
  b += c;
}
System.out.printf("a=%d, b=%d, c=%d%n", a, b, c);

#3


0  

No, an if statement can only have one then. If blah then blah. I'm not 100% positive, thought it probably also depends on the coding language. Though in java, no.

不,if语句只能有一个。如果等等等等等等等等。我不是100%肯定,认为它可能还取决于编码语言。虽然在java中,没有。

#4


0  

Everything within the if-block gets executed:

if块中的所有内容都会被执行:

public class test {

    if (condition) {
        methodA();
        methodB();
    }
}

Here, because both methods are in between the curly brackets ({}), both methodA() and methodB() get executed.

这里,因为两个方法都在大括号({})之间,所以方法A()和方法B()都被执行。

Introducing exception handling changes the answer depending on how you write the exception. If you write it so that it breaks out of the current code block at methodA(), then methodB() will not be executed. Of course, you would have to be purposely writing it this way.

引入异常处理会根据您编写异常的方式更改答案。如果你编写它以便它在methodA()的当前代码块中突破,那么将不会执行methodB()。当然,你必须故意以这种方式写作。

#5


0  

This has nothing to do with if statements.
What you want is a try-catch block:

这与if语句无关。你想要的是一个try-catch块:

 if (condition) {
     try {
         // some code that may throw exceptions
     } catch (SomeException e) {
         //
     }
     // some more code, which can also be in anoth try-catch
 }