我刚开始在一堂课中学习Java。这个任务是制作一个Rock,Paper and Scissors游戏,这里是我的源代码。我已经把它翻过来了,但还没有收到分数。如何在Java中使用if有条件地循环语句?
package rockpaperscissors;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Create scanner input
Scanner input = new Scanner (System.in);
// Introduce user to game
System.out.println("Welcome to the Rock, Paper and Scissors game");
// Ask the user for their move
System.out.println("\nPlease select a move\nEnter 0 for Scissors, 1 "
+ "for Rock and 2 for Paper");
int userMove = input.nextInt();
// Ensure that userMove is between 0 and 2
if (userMove > 2 || userMove < 0) {
System.out.println("Invalid entry the result of this game will not "
+ "be accurate! Please retry using either 0 for Scissors, 1"
+ " for Rock and 2 for Paper\n");
}
// Generate computerMove using java.util.Random
// Create instance first
Random MyRandom = new Random();
//Now generate number
int computerMove = MyRandom.nextInt(3);
System.out.print("The computer played " + computerMove + "\nRemember"
+ " 0 stands for Scissors, 1 is for Rock and 2 is for paper\n");
// Determine draw result
if (userMove == computerMove) {
System.out.println("\nYou played " + userMove + ", The computer "
+ "played " + computerMove + " The game is a draw!");
}
// Determine computer win
else if(userMove == 0 && computerMove == 1) {
System.out.println("\nYou played " + userMove + ", The computer "
+ "played " + computerMove + " The Computer Wins!");
}
else if(userMove == 2 && computerMove == 0) {
System.out.println("\nYou played " + userMove + ", The computer "
+ "played " + computerMove + " The Computer Wins!");
}
else if(userMove == 1 && computerMove == 2) {
System.out.println("\nYou played " + userMove + ", The computer "
+ "played " + computerMove + " The Computer Wins!");
}
/* Determine User win, as no other moves can result in computer win all
other combinations will result in user win
*/
else {
System.out.print("\nYou played " + userMove + ", The computer "
+ "played " + computerMove + " You Win!");
}
}
}
我有上获取节目的数量来重新问,如果数量大于2或小于0的麻烦,它会说“无效的条目中的结果将是无效的。 ..“但它仍然会用程序的其余部分执行一个无效的数字。
如何让程序停止并提示用户输入有效的数字,然后执行程序?此外,这是我所做的第三个程序,所以任何其他建议,将不胜感激。
感谢
我不认为你阅读的整个问题。 (提示:新程序员并不总是使用正确的术语。) – ajb
我正在处理它... :) –
对于任何混淆抱歉,我真的不明白这些条款。 我们所讨论的唯一循环是,虽然老师不在意我不知道该怎么做,但我只想知道我的个人知识 – user3344771