好的,嗨,每个人。这是我第一次访问stackoverflow,我在这里进行了很多搜索,所以我认为这是一个获得答案的好地方。我是目前在学校的初学者。我正在完成创建摇滚,纸张,剪刀游戏的任务。到目前为止,我觉得我的代码很好,我编译它但它根本不会运行。看看:
import java.util.Scanner;
public class RPSGame {
public static void gameModeSelect ()
{
System.out.println("Welcome to Rock, Paper, Scissors 1.0 !\n Please select your game mode: ");
System.out.println("1. Player vs. Computer\n 2. Player vs. Player ");
}
public static void winLoss ()
{
int P1 = 0, P2 = 0;
if (P1 == 'P' && P2 == 'R'){
System.out.println("Paper covers rock!\nPlayer one wins!");
} else if (P1 == 'R' && P2 == 'P'){
System.out.println("Paper covers rock!\nPlayer two wins!");
}
if (P1 == 'R' && P2 == 'S'){
System.out.println("Rock breaks scissors!\nPlayer one wins!");
} else if (P1 == 'S' && P2 == 'R'){
System.out.println("Rock breaks scissors!\nPlayer two wins!");
}
if (P1 == 'S' && P2 == 'P'){
System.out.println("Scissor cuts paper!\nPlayer one wins!");
} else if (P1 == 'P' && P2 == 'S'){
System.out.println("Scissor cuts paper!\nPlayer two wins!");
}
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int P1;
P1 = 0;
int P2;
P2 = 0;
int modeSelect;
modeSelect = keyboard.nextInt();
gameModeSelect ();
if (modeSelect == 1){
System.out.println("Oops, this feature in currently unavailable. Play with a friend for now :-)");
} else if (modeSelect == 2){
System.out.println("Rules of the game: R = Rock, P = Paper, S = Scissors\n Good luck! ");
System.out.println("Player one: Enter your move");
P1 = keyboard.nextInt();
System.out.println("Player two: Enter your move");
P2 = keyboard.nextInt();
} else if (modeSelect > 2){
winLoss();
}
}
}
它编译没有错误但没有运行,但是当我在运行部分输入任何内容时,我收到此错误:
run:
S
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at RPSGame.main(RPSGame.java:41)
C:\Users\AVLG2\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 12 seconds)
我被困在这里,我的任务明天到期。有什么建议?非常感谢。顺便说一句,很高兴成为stackoverflow社区的一员!!
答案 0 :(得分:2)
您没有看到任何内容,因为您调用了$pdo->lastInsertId();
方法
main
在打印任何东西之前。此调用将停止主线程,直到用户提供一些要读取的数据。如果数据类型为modeSelect = keyboard.nextInt();
,则控制流将移动到下一行,如果不是int
将抛出异常(就像您的情况int
无效{ {1}})。
让用户知道打印消息的内容,这解释了程序正在等待他的输入:
R
此外,当您要求用户输入字符时,您无法使用int
,因为System.out.print("Please provide game mode (<mode explanation here>): ");
modeSelect = keyboard.nextInt();
不是nextInt()
。请改用R
。
如有问题,请阅读:
How do I compare strings in Java?
Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods
What is a stack trace, and how can I use it to debug my application errors?
答案 1 :(得分:2)
boolean x;
for(int i=0;i<=holdN.length;i++){
...
}
return x;
答案 2 :(得分:1)
如前所述,Scanner#nextInt()
只能解析用户输入的整数。对于角色,它会引发异常。
此处您的代码稍作修改,以使其有效:
public class RPSGame {
public static void gameModeSelect() {
System.out.println("Welcome to Rock, Paper, Scissors 1.0 !\n Please select your game mode: ");
System.out.println("1. Player vs. Computer\n 2. Player vs. Player ");
}
public static void winLoss() {
int P1 = 0, P2 = 0;
if (P1 == 'P' && P2 == 'R') {
System.out.println("Paper covers rock!\nPlayer one wins!");
} else if (P1 == 'R' && P2 == 'P') {
System.out.println("Paper covers rock!\nPlayer two wins!");
}
if (P1 == 'R' && P2 == 'S') {
System.out.println("Rock breaks scissors!\nPlayer one wins!");
} else if (P1 == 'S' && P2 == 'R') {
System.out.println("Rock breaks scissors!\nPlayer two wins!");
}
if (P1 == 'S' && P2 == 'P') {
System.out.println("Scissor cuts paper!\nPlayer one wins!");
} else if (P1 == 'P' && P2 == 'S') {
System.out.println("Scissor cuts paper!\nPlayer two wins!");
}
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String P1 = null;
String P2 = null;
int modeSelect;
gameModeSelect();
modeSelect = keyboard.nextInt();
if (modeSelect == 1) {
System.out.println("Oops, this feature in currently unavailable. Play with a friend for now :-)");
} else if (modeSelect == 2) {
System.out.println("Rules of the game: R = Rock, P = Paper, S = Scissors\n Good luck! ");
System.out.println("Player one: Enter your move");
P1 = keyboard.next();
System.out.println("Player two: Enter your move");
P2 = keyboard.next();
} else if (modeSelect > 2) {
winLoss();
}
}
}
缺少评估,但我想这取决于你。