代码岩石剪刀纸游戏

时间:2015-07-21 17:46:12

标签: java

我编写了一个岩石剪刀纸游戏,但它没有显示我想要的结果。代码应该显示谁赢了比赛,不幸的是它没有用。

import javax.swing.JOptionPane;

public class rsp {

public static void main(String[] args){

    int computer;
    double random = 0;
    char comp = ' ';
    String statement = "";

    random = Math.random() * 10;
    computer = (int) random;

        switch (computer)
        {
            case 1: 
            case 4:
            case 7:
                comp = 'r'; // rock
                break;
            case 2:
            case 5:
            case 8:
                comp = 's'; // sissors
                break;
            case 3:
            case 6:
            case 9:
                comp = 'p'; // paper
                break;
            default : statement = "this";
        } //end of switch statement

    String u;
    u = JOptionPane.showInputDialog("ROCK, SISSORS, PAPER?");
    char player = u.toLowerCase().charAt(0);

        if (computer == 'r' & player == 'r')
        {
            statement = ("COMPUTER : ROCK. DRAW");
        }
        if (computer == 'r' & player == 's')
        {
            statement = ("COMPUTER : ROCK. LOST");
        }
        if (computer == 'r' & player == 'p')
        {
            statement = ("COMPUTER : ROCK. WON");
        }
        if (computer == 's' & player == 'r')
        {
            statement = ("COMPUTER : SISSORS. WON");
        }
        if (computer == 's' & player == 's')
        {
            statement = ("COMPUTER : SISSORS. DRAW");
        }
        if (computer == 's' & player == 'p')
        {
            statement = ("COMPUTER : SISSORS. LOST");
        }
        if (computer == 'p' & player == 'r')
        {
            statement = ("COMPUTER : PAPER. LOST");
        }
        if (computer == 'p' & player == 's')
        {
            statement = ("COMPUTER : PAPER. WON");
        }
        if (computer == 'p' & player == 'p')
        {
            statement = ("COMPUTER : PAPER. DRAW");
        }
            System.out.println (statement);
    }
}

1 个答案:

答案 0 :(得分:3)

当您选择计算机的选择时,您可以在变量comp中进行设置。但是,当您执行if条件时,请与computer进行比较。

if更改为

if(comp == 'r' && player == 'r')

它应该有用。