【发布时间】:2015-09-22 23:58:42
【问题描述】:
我必须为我的班级制作一个非常简单的石头、纸、剪刀游戏。我通常对这些任务很满意,但我已经在这个特定的任务上工作了大约 8 个小时,我知道它不应该花这么长时间,而且我必须遗漏一些东西。
我们正处于课程的早期阶段,我想确保我正确地完成了作业。我们已经讨论过整数、双精度、布尔表达式、字符和字符串。我们刚开始使用 if 和 else if 语句,所以我想避免实现我们尚未在课堂上介绍的技术。
import java.util.Scanner;
public class RPSFinal {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String user;
char R, P, S;
R = '1';
P = '2';
S = '3';
int compInt = (int) (3*Math.random())+1;
System.out.print("Rock, Paper, Scissors-Enter a choice R/P/S: ");
user = keyboard.nextLine();
user = user.toUpperCase();
char chUser = user.charAt(0);
if (chUser == compInt)
{
System.out.println("We chose the same item-try again");
}
else if (chUser == R)
{
if (compInt == P)
System.out.println("1");
else if (compInt == S)
System.out.println("2");
}
else if (chUser == P)
{
if (compInt == R)
System.out.println("3");
else if (compInt == S)
System.out.println("4");
}
else if (chUser == S)
{
if (compInt == R)
System.out.println("5");
else if (compInt == P)
System.out.println("6");
}
else
System.out.println("User error.");
}
}
编辑
我不确定我需要做什么才能使程序正常运行。每次我输入 r、p 或 s 时,程序只会返回“用户错误”。
【问题讨论】:
-
你的问题是什么?
-
如果您正在寻找代码审查,您可以考虑使用CodeReview
-
我只是想指出您正在提示用户输入“R”、“P”或“S”,但是将他们的选择与“1”、“2”和“3”进行比较'。
-
我不确定我需要做什么才能让程序正常工作。每次我输入 r、p 或 s 时,程序只会返回“用户错误”
-
更改此设置的最简单方法是建议用户输入整数而不是字符 (
System.out.print("Rock, Paper, Scissors-Enter a choice 1 (=R) / 2 (=P) / 3 (=S): ");)。
标签: java