Java 条件混乱? (初学者)
Java Conditions Confusion? (Beginner)
这个程序的目的是制作剪刀石头布游戏。我已经成功地做到了,但是无论我尝试什么,我都无法让它循环。我试过了:
while (index = 0)
while (index < gamesCount)
然而,虽然我的索引为 0 并且我的条件为 while (index != 0)
,但它似乎是运行程序的唯一条件,但无论如何它都不会循环。我怎样才能让我的游戏循环?
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random randomGen = new Random();
//Variables
String player1;
int cpu;
int start = 1;
int end = 3;
int index = 0;
// 1 = Rock | 2 = Scissors | 3 = Paper
//Code
System.out.println("Welcome to Rock, Paper, Scissors!");
while (index != 0) {
System.out.print("Rock, Paper, or Scissors?: ");
player1 = in.nextLine();
cpu = randomGen.nextInt(3);
System.out.println(cpu);
if (player1.equals("Rock") && (cpu == 2)) {
System.out.println("You lose!");
} else if (player1.equals("Rock") && (cpu == 1)) {
System.out.println("You win!");
} else if (player1.equals("Rock") && (cpu == 0)) {
System.out.println("Draw!");
}
// --------------------
if (player1.equals("Scissors") && (cpu == 2)) {
System.out.println("Draw!");
} else if (player1.equals("Scissors") && (cpu == 1)) {
System.out.println("You win!");
} else if (player1.equals("Scissors") && (cpu == 0)) {
System.out.println("You lose!");
}
//---------------------
if (player1.equals("Paper") && (cpu == 2)) {
System.out.println("You lose!");
} else if (player1.equals("Paper") && (cpu == 1)) {
System.out.println("You win!");
} else if (player1.equals("Paper") && (cpu == 0)) {
System.out.println("Draw!");
}
}
}
}
两个错误:
while (index != 0);
这是整个循环。它要么在 { } 块(你没有)的末尾结束,要么在第一个结束;紧接在语句之后。
尽管更正此问题,它仍然不会循环:
int index = 0;
// 1 = Rock | 2 = Scissors | 3 = Paper
//Code
System.out.println("Welcome to Rock, Paper, Scissors!");
while (index != 0);
index = 0,所以 (index != 0) 永远不会 return true。
您的索引变量设置为值 0。
你的 while 循环说
while (index != 0);
这意味着,虽然索引不为 0,但 运行 我的代码。问题是您的代码永远不会 运行 因为您的索引值始终为 0。
尝试将其更改为另一个值(例如 5),它现在应该可以工作了。
:)
您将索引变量设置为 0。while 循环的条件是,如果索引不等于 0,则执行循环中的代码。由于您的索引等于 0,因此不会执行循环中的指令。此外,您还需要更新循环中的索引变量,以便在满足您查找的条件时,代码将停止循环。
即:
int gamesPlayed = 0;
int gamesRequested = 3; // or get this from the user
while (gamesPlayed < gamesRequested){
String player1Choice = in.nextLine();
if(!"".equals(player1)){
// your code
gamesPlayed++;
} else {
System.out.print("Rock, Paper, or Scissors?: ");
}
}
这个程序的目的是制作剪刀石头布游戏。我已经成功地做到了,但是无论我尝试什么,我都无法让它循环。我试过了:
while (index = 0)
while (index < gamesCount)
然而,虽然我的索引为 0 并且我的条件为 while (index != 0)
,但它似乎是运行程序的唯一条件,但无论如何它都不会循环。我怎样才能让我的游戏循环?
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random randomGen = new Random();
//Variables
String player1;
int cpu;
int start = 1;
int end = 3;
int index = 0;
// 1 = Rock | 2 = Scissors | 3 = Paper
//Code
System.out.println("Welcome to Rock, Paper, Scissors!");
while (index != 0) {
System.out.print("Rock, Paper, or Scissors?: ");
player1 = in.nextLine();
cpu = randomGen.nextInt(3);
System.out.println(cpu);
if (player1.equals("Rock") && (cpu == 2)) {
System.out.println("You lose!");
} else if (player1.equals("Rock") && (cpu == 1)) {
System.out.println("You win!");
} else if (player1.equals("Rock") && (cpu == 0)) {
System.out.println("Draw!");
}
// --------------------
if (player1.equals("Scissors") && (cpu == 2)) {
System.out.println("Draw!");
} else if (player1.equals("Scissors") && (cpu == 1)) {
System.out.println("You win!");
} else if (player1.equals("Scissors") && (cpu == 0)) {
System.out.println("You lose!");
}
//---------------------
if (player1.equals("Paper") && (cpu == 2)) {
System.out.println("You lose!");
} else if (player1.equals("Paper") && (cpu == 1)) {
System.out.println("You win!");
} else if (player1.equals("Paper") && (cpu == 0)) {
System.out.println("Draw!");
}
}
}
}
两个错误:
while (index != 0);
这是整个循环。它要么在 { } 块(你没有)的末尾结束,要么在第一个结束;紧接在语句之后。
尽管更正此问题,它仍然不会循环:
int index = 0;
// 1 = Rock | 2 = Scissors | 3 = Paper
//Code
System.out.println("Welcome to Rock, Paper, Scissors!");
while (index != 0);
index = 0,所以 (index != 0) 永远不会 return true。
您的索引变量设置为值 0。 你的 while 循环说
while (index != 0);
这意味着,虽然索引不为 0,但 运行 我的代码。问题是您的代码永远不会 运行 因为您的索引值始终为 0。 尝试将其更改为另一个值(例如 5),它现在应该可以工作了。
:)
您将索引变量设置为 0。while 循环的条件是,如果索引不等于 0,则执行循环中的代码。由于您的索引等于 0,因此不会执行循环中的指令。此外,您还需要更新循环中的索引变量,以便在满足您查找的条件时,代码将停止循环。 即:
int gamesPlayed = 0;
int gamesRequested = 3; // or get this from the user
while (gamesPlayed < gamesRequested){
String player1Choice = in.nextLine();
if(!"".equals(player1)){
// your code
gamesPlayed++;
} else {
System.out.print("Rock, Paper, or Scissors?: ");
}
}