Java 如果第一个是真的,don';不要跑第二个
您好,我是Java新手,在读了一点关于Java的内容后,我尝试编写一个小程序。程序包含两个if堆栈,我想知道其中一个是否解析程序结束,而不读取第二个。 下面是我想出的代码: Scanner myScanner=新扫描仪(System.in)Java 如果第一个是真的,don';不要跑第二个,java,Java,您好,我是Java新手,在读了一点关于Java的内容后,我尝试编写一个小程序。程序包含两个if堆栈,我想知道其中一个是否解析程序结束,而不读取第二个。 下面是我想出的代码: Scanner myScanner=新扫描仪(System.in) 我想如果“独立如果”为真,程序结束而不运行第二个,有人能帮我吗?使用if/else if if(independent1.equals(userInput)){ .... } else if(independent2.equals(userInput)){
我想如果“独立如果”为真,程序结束而不运行第二个,有人能帮我吗?使用
if/else if
if(independent1.equals(userInput)){
....
} else if(independent2.equals(userInput)){
....
} else if(independent3.equals(userInput)){
...
}
如果使用,还可以使用带字符串的switch语句。他们是被介绍的
使用
if/else if
if(independent1.equals(userInput)){
....
} else if(independent2.equals(userInput)){
....
} else if(independent3.equals(userInput)){
...
}
如果使用,还可以使用带字符串的switch语句。他们是被介绍的
使用
if/else if
if(independent1.equals(userInput)){
....
} else if(independent2.equals(userInput)){
....
} else if(independent3.equals(userInput)){
...
}
如果使用,还可以使用带字符串的switch语句。他们是被介绍的
使用
if/else if
if(independent1.equals(userInput)){
....
} else if(independent2.equals(userInput)){
....
} else if(independent3.equals(userInput)){
...
}
如果使用,还可以使用带字符串的switch语句。他们是被介绍的
您还可以使用开关箱:
switch(expression){
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case statements.
default : //Optional
//Statements
}
您还可以使用开关箱:
switch(expression){
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case statements.
default : //Optional
//Statements
}
您还可以使用开关箱:
switch(expression){
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case statements.
default : //Optional
//Statements
}
您还可以使用开关箱:
switch(expression){
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case statements.
default : //Optional
//Statements
}
像下面这样尝试
boolean b = false;
if(independent1.equals(userInput)){
System.out.println("You should rely on yourself more than on mystic forces like me");
b = true;
} else if(independent2.equals(userInput)) {
System.out.println("You should rely on yourself more than on mystic forces like me");
b = true;
}else if(independent3.equals(userInput)){
b = true;
System.out.println("You should rely on yourself more than on mystic forces like me");
}
if(!b){
Random myRandom = new Random();
randomNumber =myRandom.nextInt(100)+1;
if (randomNumber >= 50){
System.out.println(randomNumber);
System.out.println("Yes");
} else {
System.out.println(randomNumber);
System.out.println("No!");
}
像下面这样尝试
boolean b = false;
if(independent1.equals(userInput)){
System.out.println("You should rely on yourself more than on mystic forces like me");
b = true;
} else if(independent2.equals(userInput)) {
System.out.println("You should rely on yourself more than on mystic forces like me");
b = true;
}else if(independent3.equals(userInput)){
b = true;
System.out.println("You should rely on yourself more than on mystic forces like me");
}
if(!b){
Random myRandom = new Random();
randomNumber =myRandom.nextInt(100)+1;
if (randomNumber >= 50){
System.out.println(randomNumber);
System.out.println("Yes");
} else {
System.out.println(randomNumber);
System.out.println("No!");
}
像下面这样尝试
boolean b = false;
if(independent1.equals(userInput)){
System.out.println("You should rely on yourself more than on mystic forces like me");
b = true;
} else if(independent2.equals(userInput)) {
System.out.println("You should rely on yourself more than on mystic forces like me");
b = true;
}else if(independent3.equals(userInput)){
b = true;
System.out.println("You should rely on yourself more than on mystic forces like me");
}
if(!b){
Random myRandom = new Random();
randomNumber =myRandom.nextInt(100)+1;
if (randomNumber >= 50){
System.out.println(randomNumber);
System.out.println("Yes");
} else {
System.out.println(randomNumber);
System.out.println("No!");
}
像下面这样尝试
boolean b = false;
if(independent1.equals(userInput)){
System.out.println("You should rely on yourself more than on mystic forces like me");
b = true;
} else if(independent2.equals(userInput)) {
System.out.println("You should rely on yourself more than on mystic forces like me");
b = true;
}else if(independent3.equals(userInput)){
b = true;
System.out.println("You should rely on yourself more than on mystic forces like me");
}
if(!b){
Random myRandom = new Random();
randomNumber =myRandom.nextInt(100)+1;
if (randomNumber >= 50){
System.out.println(randomNumber);
System.out.println("Yes");
} else {
System.out.println(randomNumber);
System.out.println("No!");
}
我不确定,但以下可能是合适的
userInput = myScanner.nextLine(); // Entire line maybe?
if (userInput.contains(independent1)) {
...
} else if (userInput.contains(independent2)) {
...
} else if (userInput.contains(independent3)) {
...
} else {
// Maybe you meant the rest to go in here, as alternative.
Random ...
}
我不确定,但以下可能是合适的
userInput = myScanner.nextLine(); // Entire line maybe?
if (userInput.contains(independent1)) {
...
} else if (userInput.contains(independent2)) {
...
} else if (userInput.contains(independent3)) {
...
} else {
// Maybe you meant the rest to go in here, as alternative.
Random ...
}
我不确定,但以下可能是合适的
userInput = myScanner.nextLine(); // Entire line maybe?
if (userInput.contains(independent1)) {
...
} else if (userInput.contains(independent2)) {
...
} else if (userInput.contains(independent3)) {
...
} else {
// Maybe you meant the rest to go in here, as alternative.
Random ...
}
我不确定,但以下可能是合适的
userInput = myScanner.nextLine(); // Entire line maybe?
if (userInput.contains(independent1)) {
...
} else if (userInput.contains(independent2)) {
...
} else if (userInput.contains(independent3)) {
...
} else {
// Maybe you meant the rest to go in here, as alternative.
Random ...
}
看看一个“else if()”而不是许多“if”怎么样?if(independent1.equals(userInput))System.out.println(“bla”);else if(independent2.equals(userInput))System.out.println(“foo”);看看一个“else if()”而不是许多“if”怎么样?if(independent1.equals(userInput))System.out.println(“bla”);else if(independent2.equals(userInput))System.out.println(“foo”);看看一个“else if()”而不是许多“if”怎么样?if(independent1.equals(userInput))System.out.println(“bla”);else if(independent2.equals(userInput))System.out.println(“foo”);看看一个“else if()”而不是许多“if”怎么样?if(independent1.equals(userInput))System.out.println(“bla”);else if(independent2.equals(userInput))System.out.println(“foo”);使用
开关
@nachokk if/else更灵活,两者都很有用。您的评论没有效果。@Zaphod42哪种方式更灵活?我认为这是因为他编辑..+1 for edit使用开关
@nachokk if/else更灵活,两者都很有用。您的评论没有效果。@Zaphod42哪种方式更灵活?我认为这是因为他编辑..+1 for edit使用开关
@nachokk if/else更灵活,两者都很有用。您的评论没有效果。@Zaphod42哪种方式更灵活?我认为这是因为他编辑..+1 for edit使用开关
@nachokk if/else更灵活,两者都很有用。您的评论没有效果。@Zaphod42哪种方式更灵活?我想这是因为他编辑…+1编辑