调用在Java中的另一个循环中创建的变量,以及如何在do…while循环中具有多个条件
我正在为学校的班级制作一个石头剪刀游戏。我把所有的事情都安排好了,除了当一个无效的选择被回答时,设置一些东西来重申这个问题。我认为do…while语句在这里是最好的,但我意识到我在循环的“do”部分调用一个变量。因此,由于它从另一个循环调用某些内容,所以它找不到该变量。另外,在我的while语句中,我是否正确地列出了可接受的变量?我很确定那部分是我编造的 非常感谢你们的帮助调用在Java中的另一个循环中创建的变量,以及如何在do…while循环中具有多个条件,java,Java,我正在为学校的班级制作一个石头剪刀游戏。我把所有的事情都安排好了,除了当一个无效的选择被回答时,设置一些东西来重申这个问题。我认为do…while语句在这里是最好的,但我意识到我在循环的“do”部分调用一个变量。因此,由于它从另一个循环调用某些内容,所以它找不到该变量。另外,在我的while语句中,我是否正确地列出了可接受的变量?我很确定那部分是我编造的 非常感谢你们的帮助 import java.util.Scanner; public class RockPaperScissorsTest
import java.util.Scanner;
public class RockPaperScissorsTest {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System. in );
do {
System.out.println("Player 1, choose 1 for rock, 2 for paper, or 3 for scissors.");
int P1 = input.nextInt();
} while (P != 1; P1 != 2; P1 != 3);
System.out.println("");
System.out.println("");
System.out.println("");
do {
System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
int P2 = input.nextInt();
} while (P2 != 1; P2 != 2; P2 != 3);
if (P1 == 1 & P2 == 1)
System.out.println("It's a tie!");
if (P1 == 1 & P2 == 2)
System.out.println("Player 2 wins!");
if (P1 == 1 & P2 == 3)
System.out.println("Player 1 wins!");
if (P1 == 2 & P2 == 1)
System.out.println("Player 1 wins!");
if (P1 == 2 & P2 == 2)
System.out.println("It's a tie!");
if (P1 == 2 & P2 == 3)
System.out.println("Player 2 wins!");
if (P1 == 3 & P2 == 1)
System.out.println("Player 2 wins!");
if (P1 == 3 & P2 == 2)
System.out.println("Player 1 wins");
if (P1 == 3 & P2 == 3)
System.out.println("It's a tie!");
}
}
有几件事突然出现:
&
,而不是&
(&
是一个数学运算符,执行按位布尔或运算)
所以
P2
:
do {
System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
int P2 = input.nextInt();
// ^----------------------------- here
} while (/*...*/);
…将仅在循环中可用,而不是在循环之后。您需要在循环之外声明该变量。(当然,对于P1
,也是如此。)在这种情况下,最好的位置是函数的顶部:
int P1, P2;
while
的条件)中,要组合多个条件,可以使用逻辑运算符&&
(“and”)和|
(“or”),而不是分号(;
)。例如:
do {
System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
P2 = input.nextInt();
} while (P2 != 1 && P2 != 2 && P2 != 3);
// ^^---------^^---- "If P2 is not 1 AND P2 is not 2 AND P2 is not 3"
if (P1 == P2) {
System.out.println("It's a tie!");
}
else if (P1 == 1) {
if (P2 == 2) {
System.out.println("Player 2 wins!");
}
else {
System.out.println("Player 1 wins!");
}
}
else if (P1 == 2) {
if (P2 == 1) {
System.out.println("Player 1 wins!");
}
else {
System.out.println("Player 2 wins!");
}
}
else { // P1 == 3
if (P2 == 1) {
System.out.println("Player 2 wins!");
}
else {
System.out.println("Player 1 wins");
}
}
P1==1
,P2==2
,等等)。您可以通过检查P1==P2
来测试平局,也可以通过使用else
来显著减少条件检查的数量,例如:
do {
System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
P2 = input.nextInt();
} while (P2 != 1 && P2 != 2 && P2 != 3);
// ^^---------^^---- "If P2 is not 1 AND P2 is not 2 AND P2 is not 3"
if (P1 == P2) {
System.out.println("It's a tie!");
}
else if (P1 == 1) {
if (P2 == 2) {
System.out.println("Player 2 wins!");
}
else {
System.out.println("Player 1 wins!");
}
}
else if (P1 == 2) {
if (P2 == 1) {
System.out.println("Player 1 wins!");
}
else {
System.out.println("Player 2 wins!");
}
}
else { // P1 == 3
if (P2 == 1) {
System.out.println("Player 2 wins!");
}
else {
System.out.println("Player 1 wins");
}
}
enum
s。在我修改您的逻辑时,我只是相信您的逻辑是正确的,因为我不知道查看代码时1
、2
或3
是什么。(显然,我可以在上面查看您输出的消息。)使用enum
将使代码更加清晰。(if(P2==RPS.Paper)
)我建议学习一些Java教程。Oracle。有几件事突然出现:
&
,而不是&
(&
是一个数学运算符,执行按位布尔或运算)
所以
P2
:
do {
System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
int P2 = input.nextInt();
// ^----------------------------- here
} while (/*...*/);
…将仅在循环中可用,而不是在循环之后。您需要在循环之外声明该变量。(当然,对于P1
,也是如此。)在这种情况下,最好的位置是函数的顶部:
int P1, P2;
while
的条件)中,要组合多个条件,可以使用逻辑运算符&&
(“and”)和|
(“or”),而不是分号(;
)。例如:
do {
System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
P2 = input.nextInt();
} while (P2 != 1 && P2 != 2 && P2 != 3);
// ^^---------^^---- "If P2 is not 1 AND P2 is not 2 AND P2 is not 3"
if (P1 == P2) {
System.out.println("It's a tie!");
}
else if (P1 == 1) {
if (P2 == 2) {
System.out.println("Player 2 wins!");
}
else {
System.out.println("Player 1 wins!");
}
}
else if (P1 == 2) {
if (P2 == 1) {
System.out.println("Player 1 wins!");
}
else {
System.out.println("Player 2 wins!");
}
}
else { // P1 == 3
if (P2 == 1) {
System.out.println("Player 2 wins!");
}
else {
System.out.println("Player 1 wins");
}
}
P1==1
,P2==2
,等等)。您可以通过检查P1==P2
来测试平局,也可以通过使用else
来显著减少条件检查的数量,例如:
do {
System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
P2 = input.nextInt();
} while (P2 != 1 && P2 != 2 && P2 != 3);
// ^^---------^^---- "If P2 is not 1 AND P2 is not 2 AND P2 is not 3"
if (P1 == P2) {
System.out.println("It's a tie!");
}
else if (P1 == 1) {
if (P2 == 2) {
System.out.println("Player 2 wins!");
}
else {
System.out.println("Player 1 wins!");
}
}
else if (P1 == 2) {
if (P2 == 1) {
System.out.println("Player 1 wins!");
}
else {
System.out.println("Player 2 wins!");
}
}
else { // P1 == 3
if (P2 == 1) {
System.out.println("Player 2 wins!");
}
else {
System.out.println("Player 1 wins");
}
}
enum
s。在我修改您的逻辑时,我只是相信您的逻辑是正确的,因为我不知道查看代码时1
、2
或3
是什么。(显然,我可以在上面查看您输出的消息。)使用enum
将使代码更加清晰。(if(P2==RPS.Paper)
)我建议学习一些Java教程。Oracle。有几件事突然出现:
&
,而不是&
(&
是一个数学运算符,执行按位布尔或运算)
所以
P2
:
do {
System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
int P2 = input.nextInt();
// ^----------------------------- here
} while (/*...*/);
…将仅在循环中可用,而不是在循环之后。您需要在循环之外声明该变量。(当然,对于P1
,也是如此。)在这种情况下,最好的位置是函数的顶部:
int P1, P2;
while
的条件)中,要组合多个条件,可以使用逻辑运算符&&
(“and”)和|
(“or”),而不是分号(;
)。例如:
do {
System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
P2 = input.nextInt();
} while (P2 != 1 && P2 != 2 && P2 != 3);
// ^^---------^^---- "If P2 is not 1 AND P2 is not 2 AND P2 is not 3"
if (P1 == P2) {
System.out.println("It's a tie!");
}
else if (P1 == 1) {
if (P2 == 2) {
System.out.println("Player 2 wins!");
}
else {
System.out.println("Player 1 wins!");
}
}
else if (P1 == 2) {
if (P2 == 1) {
System.out.println("Player 1 wins!");
}
else {
System.out.println("Player 2 wins!");
}
}
else { // P1 == 3
if (P2 == 1) {
System.out.println("Player 2 wins!");
}
else {
System.out.println("Player 1 wins");
}
}
P1==1
,P2==2
,等等)。您可以通过检查P1==P2
来测试平局,也可以通过使用else
来显著减少条件检查的数量,例如:
do {
System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
P2 = input.nextInt();
} while (P2 != 1 && P2 != 2 && P2 != 3);
// ^^---------^^---- "If P2 is not 1 AND P2 is not 2 AND P2 is not 3"
if (P1 == P2) {
System.out.println("It's a tie!");
}
else if (P1 == 1) {
if (P2 == 2) {
System.out.println("Player 2 wins!");
}
else {
System.out.println("Player 1 wins!");
}
}
else if (P1 == 2) {
if (P2 == 1) {
System.out.println("Player 1 wins!");
}
else {
System.out.println("Player 2 wins!");
}
}
else { // P1 == 3
if (P2 == 1) {
System.out.println("Player 2 wins!");
}
else {
System.out.println("Player 1 wins");
}
}
enum
s。在我修改您的逻辑时,我只是相信您的逻辑是正确的,因为我不知道查看代码时1
、2
或3
是什么。(显然更高