不能得到一段时间,或者做... while 循环工作,查找多个答案。没有任何作用
Can't get a while, or do... while loop to work, looked up multiple answers. nothing works
除了循环之外的一切都有效,当被问及我是否想再玩一次时,输入 "y" 或 "n" 它会抛出一个我无法弄清楚的错误。
如果此代码格式不正确,请原谅,这是复制和粘贴。
/* © Tucker Fowler 2015
*/
package rpslizardspock;
import java.util.Scanner;
import java.util.Random;
/**
*
* @author Tucker
*/
public class RpsLizardSpock {
public static void main(String[] args) {
String userChoice;
boolean goAgain;
boolean y = true;
boolean n = false;
Scanner sc = new Scanner(System.in);
Random ra = new Random();
while (goAgain = y) {
System.out.println("Welcome to Rock, Paper, Scissors, Lizard, Spock\n\n"
+ "Please make a Choice: ");
userChoice = sc.nextLine();
String ucConv = userChoice.toUpperCase();
System.out.println("You Chose: " + userChoice);
int cpuChoice = ra.nextInt(5) + 1;
if ((cpuChoice == 1) && (((ucConv.equals("LIZARD"))) || ((ucConv.equals("SCISSORS"))))) {
System.out.println("\n\nComputer chose Rock, You Lose");
} else if ((cpuChoice == 1) && (((ucConv.equals("PAPER"))) || ((ucConv.equals("SPOCK"))))) {
System.out.println("\n\nComputer chose Rock, You Win");
} else if ((cpuChoice == 2) && (((ucConv.equals("SPOCK"))) || ((ucConv.equals("ROCK"))))) {
System.out.println("\n\nComputer chose Paper, You Lose");
} else if ((cpuChoice == 2) && (((ucConv.equals("SCISSORS"))) || ((ucConv.equals("LIZARD"))))) {
System.out.println("\n\nComputer chose Paper, You Win");
} else if ((cpuChoice == 3) && (((ucConv.equals("LIZARD"))) || ((ucConv.equals("PAPER"))))) {
System.out.println("\n\nComputer chose Scissors, You Lose");
} else if ((cpuChoice == 3) && (((ucConv.equals("SPOCK"))) || ((ucConv.equals("ROCK"))))) {
System.out.println("\n\nComputer chose Scissors, You Win");
} else if ((cpuChoice == 4) && (((ucConv.equals("SPOCK"))) || ((ucConv.equals("PAPER"))))) {
System.out.println("\n\nComputer chose Lizard, You Lose");
} else if ((cpuChoice == 4) && (((ucConv.equals("SCISSORS"))) || ((ucConv.equals("ROCK"))))) {
System.out.println("\n\nComputer chose Lizard, You Win");
} else if ((cpuChoice == 5) && (((ucConv.equals("ROCK"))) || ((ucConv.equals("SCISSORS"))))) {
System.out.println("\n\nComputer chose Spock, You Lose");
} else if ((cpuChoice == 5) && (((ucConv.equals("PAPER"))) || ((ucConv.equals("LIZARD"))))) {
System.out.println("\n\nComputer chose Spock, You Win");
} else if ((cpuChoice == 1) && (ucConv.equals("ROCK"))) {
System.out.println("\n\nComputer chose Rock also, Draw");
} else if ((cpuChoice == 2) && (ucConv.equals("PAPER"))) {
System.out.println("\n\nComputer chose Paper also, Draw");
} else if ((cpuChoice == 3) && (ucConv.equals("SCISSORS"))) {
System.out.println("\n\nComputer chose Scissors also, Draw");
} else if ((cpuChoice == 4) && (ucConv.equals("LIZARD"))) {
System.out.println("\n\nComputer chose Lizard also, Draw");
} else if ((cpuChoice == 5) && (ucConv.equals("SPOCK"))) {
System.out.println("\n\nComputer chose Spock also, Draw");
}
System.out.println("\n\n Would you like to play again? y/n");
goAgain = sc.nextBoolean();
}
}
}
我得到的错误是:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextBoolean(Scanner.java:1825)
at rpslizardspock.RpsLizardSpock.main(RpsLizardSpock.java:73)
Java Result: 1
Scanner.nextBoolean() 查找 "true" 或 "false",而不是 "y" 或 "n".
改为尝试以下方法:
goAgain=sc.next().equalsIgnoreCase("y");
这样,如果他们输入y,goAgain就变成真,否则就变成假。
此外,正如克里斯提到的,= 表示赋值。此外,您还没有为 goAgain 赋值。因此,您应该像这样给布尔值赋值
boolean goAgain=true;//true so that you can enter the loop in the first place
对于你的循环,你真的想说(而我想再说一次)
while(goAgain){...}
最后,不要将 nextLine() 与 nextInt() 和 next() 等其他东西一起使用。我建议您将 nextLine() 的使用更改为 next(),因为它只要求一个单词。
一般来说,当您使用 Scanner
时,始终调用 next()
或始终调用 nextLine()
最不容易出错。当我们使这两个一致时,修复赋值问题,并比较 y
,然后代码可以正常工作。
import java.util.Scanner;
import java.util.Random;
public class RpsLizardSpock {
public static void main(String[] args) {
String userChoice;
boolean goAgain = true;
Scanner sc = new Scanner(System.in);
Random ra = new Random();
while (goAgain) {
System.out.println("Welcome to Rock, Paper, Scissors, Lizard, Spock\n\n"
+ "Please make a Choice: ");
userChoice = sc.next();
String ucConv = userChoice.toUpperCase();
System.out.println("You Chose: " + userChoice);
int cpuChoice = ra.nextInt(5) + 1;
if ((cpuChoice == 1) && (((ucConv.equals("LIZARD"))) || ((ucConv.equals("SCISSORS"))))) {
System.out.println("\n\nComputer chose Rock, You Lose");
} else if ((cpuChoice == 1) && (((ucConv.equals("PAPER"))) || ((ucConv.equals("SPOCK"))))) {
System.out.println("\n\nComputer chose Rock, You Win");
} else if ((cpuChoice == 2) && (((ucConv.equals("SPOCK"))) || ((ucConv.equals("ROCK"))))) {
System.out.println("\n\nComputer chose Paper, You Lose");
} else if ((cpuChoice == 2) && (((ucConv.equals("SCISSORS"))) || ((ucConv.equals("LIZARD"))))) {
System.out.println("\n\nComputer chose Paper, You Win");
} else if ((cpuChoice == 3) && (((ucConv.equals("LIZARD"))) || ((ucConv.equals("PAPER"))))) {
System.out.println("\n\nComputer chose Scissors, You Lose");
} else if ((cpuChoice == 3) && (((ucConv.equals("SPOCK"))) || ((ucConv.equals("ROCK"))))) {
System.out.println("\n\nComputer chose Scissors, You Win");
} else if ((cpuChoice == 4) && (((ucConv.equals("SPOCK"))) || ((ucConv.equals("PAPER"))))) {
System.out.println("\n\nComputer chose Lizard, You Lose");
} else if ((cpuChoice == 4) && (((ucConv.equals("SCISSORS"))) || ((ucConv.equals("ROCK"))))) {
System.out.println("\n\nComputer chose Lizard, You Win");
} else if ((cpuChoice == 5) && (((ucConv.equals("ROCK"))) || ((ucConv.equals("SCISSORS"))))) {
System.out.println("\n\nComputer chose Spock, You Lose");
} else if ((cpuChoice == 5) && (((ucConv.equals("PAPER"))) || ((ucConv.equals("LIZARD"))))) {
System.out.println("\n\nComputer chose Spock, You Win");
} else if ((cpuChoice == 1) && (ucConv.equals("ROCK"))) {
System.out.println("\n\nComputer chose Rock also, Draw");
} else if ((cpuChoice == 2) && (ucConv.equals("PAPER"))) {
System.out.println("\n\nComputer chose Paper also, Draw");
} else if ((cpuChoice == 3) && (ucConv.equals("SCISSORS"))) {
System.out.println("\n\nComputer chose Scissors also, Draw");
} else if ((cpuChoice == 4) && (ucConv.equals("LIZARD"))) {
System.out.println("\n\nComputer chose Lizard also, Draw");
} else if ((cpuChoice == 5) && (ucConv.equals("SPOCK"))) {
System.out.println("\n\nComputer chose Spock also, Draw");
}
System.out.println("\n\n Would you like to play again? y/n");
goAgain = sc.next().charAt(0) == 'y';
}
}
}
除了循环之外的一切都有效,当被问及我是否想再玩一次时,输入 "y" 或 "n" 它会抛出一个我无法弄清楚的错误。
如果此代码格式不正确,请原谅,这是复制和粘贴。
/* © Tucker Fowler 2015
*/
package rpslizardspock;
import java.util.Scanner;
import java.util.Random;
/**
*
* @author Tucker
*/
public class RpsLizardSpock {
public static void main(String[] args) {
String userChoice;
boolean goAgain;
boolean y = true;
boolean n = false;
Scanner sc = new Scanner(System.in);
Random ra = new Random();
while (goAgain = y) {
System.out.println("Welcome to Rock, Paper, Scissors, Lizard, Spock\n\n"
+ "Please make a Choice: ");
userChoice = sc.nextLine();
String ucConv = userChoice.toUpperCase();
System.out.println("You Chose: " + userChoice);
int cpuChoice = ra.nextInt(5) + 1;
if ((cpuChoice == 1) && (((ucConv.equals("LIZARD"))) || ((ucConv.equals("SCISSORS"))))) {
System.out.println("\n\nComputer chose Rock, You Lose");
} else if ((cpuChoice == 1) && (((ucConv.equals("PAPER"))) || ((ucConv.equals("SPOCK"))))) {
System.out.println("\n\nComputer chose Rock, You Win");
} else if ((cpuChoice == 2) && (((ucConv.equals("SPOCK"))) || ((ucConv.equals("ROCK"))))) {
System.out.println("\n\nComputer chose Paper, You Lose");
} else if ((cpuChoice == 2) && (((ucConv.equals("SCISSORS"))) || ((ucConv.equals("LIZARD"))))) {
System.out.println("\n\nComputer chose Paper, You Win");
} else if ((cpuChoice == 3) && (((ucConv.equals("LIZARD"))) || ((ucConv.equals("PAPER"))))) {
System.out.println("\n\nComputer chose Scissors, You Lose");
} else if ((cpuChoice == 3) && (((ucConv.equals("SPOCK"))) || ((ucConv.equals("ROCK"))))) {
System.out.println("\n\nComputer chose Scissors, You Win");
} else if ((cpuChoice == 4) && (((ucConv.equals("SPOCK"))) || ((ucConv.equals("PAPER"))))) {
System.out.println("\n\nComputer chose Lizard, You Lose");
} else if ((cpuChoice == 4) && (((ucConv.equals("SCISSORS"))) || ((ucConv.equals("ROCK"))))) {
System.out.println("\n\nComputer chose Lizard, You Win");
} else if ((cpuChoice == 5) && (((ucConv.equals("ROCK"))) || ((ucConv.equals("SCISSORS"))))) {
System.out.println("\n\nComputer chose Spock, You Lose");
} else if ((cpuChoice == 5) && (((ucConv.equals("PAPER"))) || ((ucConv.equals("LIZARD"))))) {
System.out.println("\n\nComputer chose Spock, You Win");
} else if ((cpuChoice == 1) && (ucConv.equals("ROCK"))) {
System.out.println("\n\nComputer chose Rock also, Draw");
} else if ((cpuChoice == 2) && (ucConv.equals("PAPER"))) {
System.out.println("\n\nComputer chose Paper also, Draw");
} else if ((cpuChoice == 3) && (ucConv.equals("SCISSORS"))) {
System.out.println("\n\nComputer chose Scissors also, Draw");
} else if ((cpuChoice == 4) && (ucConv.equals("LIZARD"))) {
System.out.println("\n\nComputer chose Lizard also, Draw");
} else if ((cpuChoice == 5) && (ucConv.equals("SPOCK"))) {
System.out.println("\n\nComputer chose Spock also, Draw");
}
System.out.println("\n\n Would you like to play again? y/n");
goAgain = sc.nextBoolean();
}
}
}
我得到的错误是:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextBoolean(Scanner.java:1825)
at rpslizardspock.RpsLizardSpock.main(RpsLizardSpock.java:73)
Java Result: 1
Scanner.nextBoolean() 查找 "true" 或 "false",而不是 "y" 或 "n".
改为尝试以下方法:
goAgain=sc.next().equalsIgnoreCase("y");
这样,如果他们输入y,goAgain就变成真,否则就变成假。
此外,正如克里斯提到的,= 表示赋值。此外,您还没有为 goAgain 赋值。因此,您应该像这样给布尔值赋值
boolean goAgain=true;//true so that you can enter the loop in the first place
对于你的循环,你真的想说(而我想再说一次)
while(goAgain){...}
最后,不要将 nextLine() 与 nextInt() 和 next() 等其他东西一起使用。我建议您将 nextLine() 的使用更改为 next(),因为它只要求一个单词。
一般来说,当您使用 Scanner
时,始终调用 next()
或始终调用 nextLine()
最不容易出错。当我们使这两个一致时,修复赋值问题,并比较 y
,然后代码可以正常工作。
import java.util.Scanner;
import java.util.Random;
public class RpsLizardSpock {
public static void main(String[] args) {
String userChoice;
boolean goAgain = true;
Scanner sc = new Scanner(System.in);
Random ra = new Random();
while (goAgain) {
System.out.println("Welcome to Rock, Paper, Scissors, Lizard, Spock\n\n"
+ "Please make a Choice: ");
userChoice = sc.next();
String ucConv = userChoice.toUpperCase();
System.out.println("You Chose: " + userChoice);
int cpuChoice = ra.nextInt(5) + 1;
if ((cpuChoice == 1) && (((ucConv.equals("LIZARD"))) || ((ucConv.equals("SCISSORS"))))) {
System.out.println("\n\nComputer chose Rock, You Lose");
} else if ((cpuChoice == 1) && (((ucConv.equals("PAPER"))) || ((ucConv.equals("SPOCK"))))) {
System.out.println("\n\nComputer chose Rock, You Win");
} else if ((cpuChoice == 2) && (((ucConv.equals("SPOCK"))) || ((ucConv.equals("ROCK"))))) {
System.out.println("\n\nComputer chose Paper, You Lose");
} else if ((cpuChoice == 2) && (((ucConv.equals("SCISSORS"))) || ((ucConv.equals("LIZARD"))))) {
System.out.println("\n\nComputer chose Paper, You Win");
} else if ((cpuChoice == 3) && (((ucConv.equals("LIZARD"))) || ((ucConv.equals("PAPER"))))) {
System.out.println("\n\nComputer chose Scissors, You Lose");
} else if ((cpuChoice == 3) && (((ucConv.equals("SPOCK"))) || ((ucConv.equals("ROCK"))))) {
System.out.println("\n\nComputer chose Scissors, You Win");
} else if ((cpuChoice == 4) && (((ucConv.equals("SPOCK"))) || ((ucConv.equals("PAPER"))))) {
System.out.println("\n\nComputer chose Lizard, You Lose");
} else if ((cpuChoice == 4) && (((ucConv.equals("SCISSORS"))) || ((ucConv.equals("ROCK"))))) {
System.out.println("\n\nComputer chose Lizard, You Win");
} else if ((cpuChoice == 5) && (((ucConv.equals("ROCK"))) || ((ucConv.equals("SCISSORS"))))) {
System.out.println("\n\nComputer chose Spock, You Lose");
} else if ((cpuChoice == 5) && (((ucConv.equals("PAPER"))) || ((ucConv.equals("LIZARD"))))) {
System.out.println("\n\nComputer chose Spock, You Win");
} else if ((cpuChoice == 1) && (ucConv.equals("ROCK"))) {
System.out.println("\n\nComputer chose Rock also, Draw");
} else if ((cpuChoice == 2) && (ucConv.equals("PAPER"))) {
System.out.println("\n\nComputer chose Paper also, Draw");
} else if ((cpuChoice == 3) && (ucConv.equals("SCISSORS"))) {
System.out.println("\n\nComputer chose Scissors also, Draw");
} else if ((cpuChoice == 4) && (ucConv.equals("LIZARD"))) {
System.out.println("\n\nComputer chose Lizard also, Draw");
} else if ((cpuChoice == 5) && (ucConv.equals("SPOCK"))) {
System.out.println("\n\nComputer chose Spock also, Draw");
}
System.out.println("\n\n Would you like to play again? y/n");
goAgain = sc.next().charAt(0) == 'y';
}
}
}