最近了解了如何在这些类中创建类,对象,方法,如何从main方法访问它们,以及为什么这样做很重要,因此您不必总是重写代码/创建大量的代码行/保存内存/等,我遇到了一个问题。
因此,我编写了一个简单的剪刀石头布程序。本质上,每次运行该程序时,该程序都会接受用户输入-将其分配给变量,将随机数分配给另一个变量,并使用多个if语句比较两个输入以确定结果。这就是我的问题所在。
有人告诉我,与之关联的类和方法通常非常广泛,但是我觉得我用来比较两个变量的方法过于庞大,可以简化/压缩为更简单有效的代码。
我不知道该如何实现。我不确定是否要添加更多的变量,从而使比较更简单或更简单,或者省去一些事情/添加新的代码类型等。重点是,我想减少此代码矮胖且更有效。
在代码中,我有两种获取确切值的方法。我有典型的if语句,但我也做了switch语句。两者都能得到相同的结果,但我只是认为switch语句更易于阅读。
ALSO,我意识到,在制作这种简单的Rock Paper Scissors游戏时,类和方法可能没有用,但是我只是想尝试一下而已。
这是我的代码:
连同网站,您可以在以下网站上运行它:https://repl.it/@ANGELRAMIREZ6/Rock-Paper-Scissors-UPGRADED
import java.util.Scanner;
import java.util.Random;
class Main {
public static void main(String[] args) {
action object = new action();
System.out.println("\nYou will now play Rock Paper Scissors with a Computer.");
object.getUserChoice();
object.getComputerChoice();
object.compareChoices();
}
}
class action {
Scanner sc = new Scanner(System.in);
Random rand = new Random();
int computerChoice;
int userChoice;
int getUserChoice() {
System.out.println("\nDo you choose (0)Rock, (1)Paper, (2)Scissors?");
userChoice = sc.nextInt();
return userChoice;
}
int getComputerChoice() {
computerChoice = rand.nextInt(2);
return computerChoice;
}
/*
void compareChoices() {
//IF YOU PICK ROCK
if (userChoice == 0) {
if (computerChoice == 1) {
System.out.println("You lose!\nYou picked rock and the computer picked paper.");
}
if (computerChoice == 2) {
System.out.println("You win!\nYou picked rock and the computer picked scissors.");
}
if (computerChoice == 0) System.out.println("It's a draw! You both picked rock!");
}
//IF YOU PICK PAPER
if (userChoice == 1) {
if (computerChoice == 0) {
System.out.println("You win!\nYou picked paper and the computer picked rock.");
}
if (computerChoice == 2) {
System.out.println("You lose!\nYou picked paper and the computer picked scissors.");
}
if (computerChoice == 1) System.out.println("It's a draw! You both picked paper!");
}
//IF YOU PICK SCISSORS
if (userChoice == 2) {
if (computerChoice == 1) {
System.out.println("You win!\nYou picked scissors and the computer picked paper.");
}
if (computerChoice == 0) {
System.out.println("You lose!\nYou picked scissors and the computer picked rock.");
}
if (computerChoice == 2) System.out.println("It's a draw! You both picked scissors!");
}
}
*/
void compareChoices() {
switch (userChoice) {
//IF YOU PICK ROCK
case 0:
if (computerChoice == 1) {
System.out.println("You lose!\nYou picked rock and the computer picked paper.");
}
if (computerChoice == 2) {
System.out.println("You win!\nYou picked rock and the computer picked scissors.");
}
if (computerChoice == 0) System.out.println("It's a draw! You both picked rock!");
break;
//IF YOU PICK PAPER
case 1:
if (computerChoice == 0) {
System.out.println("You win!\nYou picked paper and the computer picked rock.");
}
if (computerChoice == 2) {
System.out.println("You lose!\nYou picked paper and the computer picked scissors.");
}
if (computerChoice == 1) System.out.println("It's a draw! You both picked paper!");
break;
//IF YOU PICK SCISSORS
case 2:
if (computerChoice == 1) {
System.out.println("You win!\nYou picked scissors and the computer picked paper.");
}
if (computerChoice == 0) {
System.out.println("You lose!\nYou picked scissors and the computer picked rock.");
}
if (computerChoice == 2) System.out.println("It's a draw! You both picked scissors!");
break;
}
}
}
答案 0 :(得分:0)
最好使用(0)Rock, (1)Paper, (2)Scissors
代替enum
。这将使人类更容易阅读。
您也可以在此处接收赢/输消息。这将使进一步的更改变得容易得多,因为您知道它们的位置。
在您的switch
语句中而不是使用多个if
,请创建一个采用2个参数(例如userChoice - AIChoice
)的方法,然后返回获胜者。我的意思是与其在switch
内部进行比较,不如将它们放在其他位置,而只是用方法调用替换那些if
。
我希望它能对您有所帮助:)