这是学校的作业。这是指令: 编写一个播放流行剪刀 - 岩纸游戏的程序。该程序随机生成数字1,2和3,代表剪刀,岩石和纸张。程序提示用户输入数字1,2和3,并显示一条消息,指示用户或计算机是赢,输还还是抽奖。程序应该让用户持续玩,直到用户或计算机赢得的次数超过对手的两倍。
我已经使用运行的java创建了一个但是想知道是否有更短的编写方式......
这是我写的:
import java.util.Scanner;
public class NumberOne{
public static void main(String[] args){
Scanner n = new Scanner(System.in);
int comp, player;
int point = 1, pointCom = 0, pointPlayer = 0;
System.out.println("This is a game of Scissor-Rock-Paper.\n");
while((pointPlayer != 3) && (pointCom != 3)){
System.out.println("\nEnter 1 for Scissor\nEnter 2 for Rock\nEnter 3 for Paper");
player = n.nextInt();
comp = (1 + (int)(Math.random() * 3));
if ((comp == 1) && (player == 3)){
System.out.println("Scissor vs Paper.\nYou lose!");
pointCom += point;}
else if ((comp == 2) && (player == 1)){
System.out.println("Rock vs Scissor.\nYou lose!");
pointCom += point;}
else if ((comp == 3) && (player == 2)){
System.out.println("Paper vs Rock.\nYou lose!");
pointCom += point;}
else if ((comp == 1) && (player == 2)){
System.out.println("Scissor vs Rock.\nYou win!");
pointPlayer += point;}
else if ((comp == 2) && (player == 3)){
System.out.println("Rock vs Paper.\nYou win!");
pointPlayer += point;}
else if ((comp == 3) && (player== 1)){
System.out.println("Paper vs Scissor.\nYou win!");
pointPlayer += point;}
else if ((comp == 1) && (player == 1)){
System.out.println("Scissor vs Scissor.\nIt's a draw!");}
else if ((comp == 2) && (player == 2)){
System.out.println("Rock vs Rock.\nIt's a draw!");}
else if ((comp == 3) && (player == 3)){
System.out.println("Paper vs Paper.\nIt's a draw!");}
else
System.out.println("You can only choose between 1, 2, and 3...");
}
if (pointCom >= 3)
System.out.println("\nYou LOST...");
else if (pointPlayer >= 3)
System.out.println("\nYou WON...");
}
}
答案 0 :(得分:1)
而不是使用常量System.out.println()
你可以制作两个String
变量。因为“你输了”和“你赢了”
也用于剪刀和纸张等的内容。制作一个名为String Array的字符串 “武器”或那种性质的东西,以便您可以像这样在阵列中调用它们:
if weapons.contains("paper"){
return (the you won or you lose variable)
}
答案 1 :(得分:0)
也许是这样的:
String compMove, playerMove;
switch (player) {
case 1: playerMove = "Scissor";break;
case 2: playerMove = "Rock";break;
case 3: playerMove = "Paper";break;
}
switch (comp) {
case 1: compMove = "Scissor";break;
case 2: compMove = "Rock";break;
case 3: compMove = "Paper";break;
}
System.out.print(playerMove + " vs. " + compMove);
//check for winner
答案 2 :(得分:0)
我建议将Enum类用于摇滚,纸张,剪刀。然后比较一个比较器,因为它看起来更干净,更具可读性。
public Enum Game {
ROCK(0), PAPER(1), SCISSORS(2);
private final int index;
Game(int index) {
this.index = index;
}
public int getIndex() {
return index;
}
public static Game getGame(int index) {
for (Game g : Game.values()) {
if (g.getIndex() == index) {
return g;
}
}
return null;
}
}
比较课程:
public class MyEnumComparator implements Comparator<Game>
{
public int compare(Game o1, Game o2)
{
if(o1 == o2) {
return 0;
}
if(o1 == Game.ROCK) {
return o2 == Game.PAPER ? -1 : 1;
}
if(o1 == Game.PAPER) {
return o2 == Game.SCISSORS ? -1 : 1;
}
if(o1 == Game.SCISSORS) {
return o2 == Game.ROCK ? -1 : 1;
}
}
}
然后你的主要课程将是这样的:
import java.util.Scanner;
public class NumberOne {
public static void main(String[] args){
Scanner n = new Scanner(System.in);
int comp, player;
int point = 1, pointCom = 0, pointPlayer = 0;
System.out.println("This is a game of Scissor-Rock-Paper.\n");
MyEnumComparator comparator = new MyEnumComparator();
while((pointPlayer != 3) && (pointCom != 3)){
System.out.println("\nEnter 1 for Scissor\nEnter 2 for Rock\nEnter 3 for Paper");
player = n.nextInt();
compIndex = (1 + (int)(Math.random() * 3));
Game me = Game.getGame(player);
Game computer = Game.getGame(compIndex);
// 0 means draw, 1 means you win, -1 means computer wins
int result = comparator.compare(me, computer);
if(result != 0) {
if(result == 1) {
pointPlayer++
} else {
pointComp++
}
}
}
if (pointCom >= 3)
System.out.println("\nYou LOST...");
else if (pointPlayer >= 3)
System.out.println("\nYou WON...");
}
}