摇滚,纸张,剪刀游戏没有正确保持得分

时间:2016-09-05 22:52:20

标签: java

我的摇滚,纸张,剪刀游戏存在问题。当我玩游戏时,它记录了赢或输的胜负。我试图做到这一点,它记录了一场胜利和失败,并没有增加一个赢或输的点。有什么建议吗?

import java.io.*;

public class RPS {
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static int x;

    public static void main(String[] args) throws IOException {

        int choice;
        do {
            System.out.println("Game Menu");
            System.out.println("---------------------------------------------");
            System.out.println("1. Start Game");
            System.out.println("2. Quit Program");

            choice = Integer.valueOf(in.readLine()).intValue();

            if(choice == 1) {
                System.out.println("Welcome to Rock, Paper, Scissors");
                System.out.println("---------------------------------------------");
                System.out.println("Please enter Rock, Paper or Scissors to play");

            int wins = 0, loss = 0, ties = 0;
            String user = in.readLine();
            String comp="";
            int compc=(int)(3*Math.random())+1;

            if (compc==1) {
                comp="Rock";
            }
            else if (compc==2) {
                comp="Paper";
            }
            else if (compc==3) {
                comp="Scissors";
            }

            if (user.equals("Rock") || user.equals("rock")) {
                System.out.println("You chose : Rock");
                user = "Rock";
            }
            else if (user.equals("Paper") || user.equals("paper")) {
                System.out.println("You chose : Paper");
                user = "Paper";
            }
            else if (user.equals("Scissors")|| user.equals("scissors")) {
                System.out.println("You chose : Scissors");
                user = "Scissors";
            }
            else {
                System.out.println("Error: Please type Rock, Paper or Scissors");
            }
            System.out.println("Computer: I chose " + comp);
            if(user.equals(comp)) {
                System.out.println("It's a tie!");
                ties++;
            }
            else if (user.equals("Rock")) {

                if (comp.equals("Scissors"))
                    System.out.println("Rock beats scissors, congrats you win!");
                wins++;

                 if (comp.equals("Paper"))
                     System.out.println("Paper beats rock, sorry you lost!");
                loss++;


            }
            else if (user.equals("Paper")) {

                if (comp.equals("Rock"))
                    System.out.println("Paper beats rock, congrats you win!");
                    wins++;

                 if (comp.equals("Scissors"))
                    System.out.println("Scissors beats paper, sorry you lost!");
                    loss++;         
            }
            else if (user.equals("Scissors")) {

                if (comp.equals("Paper"))
                    System.out.println("Scissors beats paper, congrats you win!");
                    wins++;
                 if (comp.equals("Rock"))
                    System.out.println("Rock beats scissors, sorry you lost!");
                    loss++;     
            }
            System.out.println("Total wins : " +wins);
            System.out.println("Total losses : " +loss);
            System.out.println("Total ties : " +ties);
            }
            if (choice == 2) {
                System.out.println("Good-bye!");
                in.close();
            }
        } while (choice !=2); 
    }
}

3 个答案:

答案 0 :(得分:1)

您需要在缩进的条件代码周围添加大括号:{}。与像Python这样的语言不同,Java中的缩进没有语义含义。

例如。

if (comp.equals("Rock"))
    System.out.println("Paper beats rock, congrats you win!");
    wins++;

与:

相同
if (comp.equals("Rock")) {
    System.out.println("Paper beats rock, congrats you win!");
}
wins++;

即。 wins无条件地增加。

尝试:

if (comp.equals("Rock")) {
    System.out.println("Paper beats rock, congrats you win!");
    wins++;
}

答案 1 :(得分:0)

if (comp.equals("Scissors"))
    System.out.println("Rock beats scissors, congrats you win!");
wins++;

if (comp.equals("Paper"))
    System.out.println("Paper beats rock, sorry you lost!");
loss++;

wins++loss++不属于if语句,因此每次都会执行。

答案 2 :(得分:0)

查看win ++和loss ++ - 无论结果如何,它们都会被自动调用...你需要将代码放在if语句中...如果java中的语句将自动包含下一行但不包含在第一行之后你会想做:

if(condition to be met){
System.out.println("BLAH");
win++
}

除非我有空间问题,否则我总是要包括大括号。我觉得它更容易阅读。

新秀的错误 - 作为最近的合作科学家 - 你会让很多人相信我。只是继续插电。祝你好运