当用户选择停止时,如何打印用户获胜,损失和关系的数量

时间:2014-10-03 03:23:11

标签: java

此时,程序显示两个选项并打印一个声明,指示用户是否赢了,计算机赢了,或者是否是平局。继续播放直到用户选择停止,然后打印用户获胜的数量,损失和关系。

import java.util.Scanner;
import java.util.Random;

public class RockPaperScissor {

    public static void main(String[] args) {

        final int MAX = 3;
        int randomNumber;
        int userChoice;
        int computerChoice;
        int choice;
        Scanner input = new Scanner(System.in);
        Random rand = new Random();//random number generated by computer

        String str, another = "y";

        Scanner scan = new Scanner(System.in);
        int rounds = 0;
        userChoice = 0;

            while (another.equalsIgnoreCase("y")) {// allows y or Y
                System.out.print("enter 1= rock 2= paper 3 = scissor(0 to quit):");
                rounds++;
                userChoice = input.nextInt();
                computerChoice = rand.nextInt(MAX) + 1;
                System.out.println("computer picked " + computerChoice);

                int lose = 0;
                int win = 0;
                int tie = 0;
                int computerScore = 0, userScore = 0;


                if (userChoice == 1 || userChoice == 2 || userChoice == 3) {
                    if (userChoice == computerChoice) {
                        System.out.println("Tie Game!");
                        System.out.println();
                        tie++;
                        //rounds++;
                    } else if (userChoice == 1) {

                        if (computerChoice == 2) {
                            //rock is covered by paper
                            System.out.println("  you lose");
                            lose++;
                            computerScore++;
                            //rounds++;
                        } else if (computerChoice == 3) {
                            //rock smashes scissors
                            System.out.println("  you win");
                            win++;
                            userScore++;
                            //rounds++;
                        }
                    } else if (userChoice == 2) {
                        if (computerChoice == 3) {
                            //paper is cut by scissors
                            System.out.println("  you lose");
                            lose++;
                            computerScore++;
                            //rounds++;
                        } else if (computerChoice == 1) {
                            //paper covers rock
                            System.out.println("  you win");
                            win++;
                            userScore++;
                            //rounds++;
                        }
                    } else if (userChoice == 3) {

                        if (computerChoice == 1) {
                            //scissors are smashed by rock
                            System.out.println("  you lose");
                            lose++;
                            computerScore++;
                            //rounds++;
                        } else if (computerChoice == 2) {
                            //scissors cut paper
                            System.out.println("  you win");
                            win++;
                            userScore++;
                            //rounds++;
                        }
                    }

                } else {
                    System.out.println("invalid number");
                }
                System.out.println();
                System.out.println("User wins: " + win + " User loses: " + lose + "User ties " + tie);
                System.out.print("Do you want to play again (y/n)? ");
                another = scan.nextLine();
            }
           }
}

3 个答案:

答案 0 :(得分:1)

你的节目似乎没问题,除了这些要点:

  • 缩进很重要!
  • 你有一个毫无意义的while(true) { /* do stuff */ break; }
  • scaninput是多余的。您可以使用它们中的任何一个来读取所有用户输入。
    • 他们相等,他们都是Scanner.new(System.in)

答案 1 :(得分:1)

你是没有布尔条件的无限循环

首先声明一个字段布尔标志并将其初始值设置为false然后当你开始游戏然后将标志值设置为true并运行while while untill标志为true当你想要停止然后简单地将flag设置为false

答案 2 :(得分:1)

完成以下代码,

import java.util.Scanner;
import java.util.Random;

公共类RockPaperScissor {

 public static void main(String[] args) {

    final int MAX = 3;
    int randomNumber = 0;
    int userChoice = 0;
    int computerChoice = 0;
    int choice = 0;
    int rounds = 0;
    int lose = 0;
    int win = 0;
    int tie = 0;
    int computerScore = 0;
    int userScore = 0;
    Scanner input = new Scanner(System.in);
    Random rand = new Random();//random number generated by computer
    String str, another = "y";

    userChoice = 0;

    while (another.equalsIgnoreCase("y")) {// allows y or Y
        System.out.print("enter 1= rock 2= paper 3 = scissor(0 to quit):");
        rounds++;
        userChoice = Integer.parseInt(input.nextLine());

        if(userChoice == 0){
            System.out.print("Do you want to play again (y/n)? ");
            another = input.nextLine();
        } else if (userChoice > 0 && userChoice < 4){
            computerChoice = rand.nextInt(MAX) + 1;
            System.out.println("computer picked " + computerChoice);

            if (userChoice == computerChoice) {
                System.out.println("Tie Game!");
                System.out.println();
                tie++;
            } else if (userChoice == 1) {

                if (computerChoice == 2) {
                    //rock is covered by paper
                    System.out.println("  you lose");
                    lose++;
                    computerScore++;
                } else if (computerChoice == 3) {
                    //rock smashes scissors
                    System.out.println("  you win");
                    win++;
                    userScore++;
                }
            } else if (userChoice == 2) {
                if (computerChoice == 3) {
                    //paper is cut by scissors
                    System.out.println("  you lose");
                    lose++;
                    computerScore++;
                } else if (computerChoice == 1) {
                    //paper covers rock
                    System.out.println("  you win");
                    win++;
                    userScore++;
                }
            } else if (userChoice == 3) {

                if (computerChoice == 1) {
                    //scissors are smashed by rock
                    System.out.println("  you lose");
                    lose++;
                    computerScore++;
                } else if (computerChoice == 2) {
                    //scissors cut paper
                    System.out.println("  you win");
                    win++;
                    userScore++;
                }
            }
        }else{
            System.out.println("invalid number");
        }
        System.out.println("computer score: " + computerScore + " You Score: " + userScore + " Rounds: " + rounds+"\n");
    }
 }

}

我从原始代码中删除了扫描和一个while循环,并定义了现有条件。

祝你好运!!!