Java Paper Rock Scissors计划

时间:2015-05-15 00:36:45

标签: java methods parameters arguments

纸岩剪刀java程序。

好吧所以我现在唯一的问题是玩家的分数在第二次循环之前不会更新。有什么建议?

再次感谢Radiodef的帮助!!

以下更新的代码.......

import java.util.Scanner;


public class RPS_Game {

public static void main(String[] args) {
    char r = 'R';
    char p = 'P';
    char s = 'S';
    char player1 = 0;
    char player2 = 0;
    int player1Score = 0;
    int player2Score = 0;
    int playCount = 0;


    Scanner scan = new Scanner(System.in);



    while(playCount < 3) {

        System.out.print("Please enter either (R)ock, (P)aper, or (S)iccors: ");
        player1 = scan.nextLine().toUpperCase().charAt(0);
        System.out.print("Please enter either (R)ock, (P)aper, or (S)iccors: ");
        player2 = scan.nextLine().toUpperCase().charAt(0);

        int winner = winningPlayer(player1, player2);   
        if(winner == 0) {
            System.out.print("\nIt's a tie.  Nobody wins!\n");
            System.out.println("\nPlayer 1: " + (player1Score += 0));
            System.out.println("\nPlayer 2: " + (player2Score += 0));
        }
        if(winner == 1) {
            System.out.print("\nPlayer 1 wins!!\n");
            System.out.println("\nPlayer 1: " + player1Score++);    
            System.out.println("\nPlayer 2: " + (player2Score += 0));
        }
        if(winner == 2) {
            System.out.print("\nPlayer 2 wins!!\n");
            System.out.println("\nPlayer 1: " + (player1Score += 0));
            System.out.println("\nPlayer 2: " + player2Score++);    
        }   
    playCount++;
    }       
}

public static int winningPlayer(int player1,  int player2) {    

    //Player 1 wins 
    int result = 0;
    if(player1 == 'R' && player2 == 'S') {      
        result = 1;
    }
    else if(player1 == 'P' && player2 == 'R') {
        result = 1;
    }   
    else if(player1 == 'S' && player2 == 'P') {
        result = 1;
    }   
    //Player 2 wins 
    else if(player2 == 'R' && player1 == 'S') {
        result = 2;
    }
    else if(player2 == 'P' && player1 == 'R') {
        result = 2;
    }   
    else if(player2 == 'S' && player1 == 'P') {
        result = 2;
    }
    return result;
}   
}

1 个答案:

答案 0 :(得分:2)

嗯,好像首先你需要将调用移到循环中:

while(playCount < 3) {
    System.out.print("Please enter either (R)ock, (P)aper, or (S)iccors: ");
    player1 = scan.nextLine().toUpperCase().charAt(0);
    System.out.print("Please enter either (R)ock, (P)aper, or (S)iccors: ");
    player2 = scan.nextLine().toUpperCase().charAt(0);

    // recompute the winner each time
    int winner = winningPlayer(player1, player2);
    ...
}

为了保持分数,你可以为每个玩家设置一个变量:

int player1Score = 0;
int player2Score = 0;

while (...) {
    ...

    if (winner == 1) {
        ++player1Score;
    }
    if (winner == 2) {
        ++player2Score;
    }
}

或者你可以做一些像使用数组的更好的东西:

int[] scores = new int[3];

while (...) {
    ...

    ++scores[ winner ];

    for (int i = 1; i < scores.length; ++i) {
        System.out.printf("Player %d score is %d.\n", i, scores[i]);
    }
}