在java中创建我的第一个项目

时间:2014-08-26 17:55:19

标签: java swing

所以,经过2个小时的Java指南,我开始制作我的第一个项目,一个简单的石头剪刀游戏,但后来我发现了一个问题。我想在每次选择石头纸或剪刀时为游戏的赢家增加1分,并且你赢了,这是我在完成之前唯一需要做的事情(我是Java新手)。

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main extends JFrame {

    private static final long serialVersionUID = 1L;

    private static String Default = "Rock,Paper,Scissors";
    private static JLabel update;
    private static JLabel userScore;
    private static JLabel pcScore;
    private static Container panel;
    private static int userCounter;

    public static int getUserCounter() {
        return userCounter;
    }

    public static void setUserCounter(int userCounter) {
        Main.userCounter = userCounter + userCounter;
    }

    public static int getPcCounter() {
        return pcCounter;
    }

    public static void setPcCounter(int pcCounter) {
        Main.pcCounter = pcCounter + pcCounter;
    }

    private static int pcCounter;
    private static final int bwidth = 90;
    private static final int bheight = 25;
    private static Random random;
    private static final String ROCK = "Rock";
    private static final String PAPER = "Paper";
    private static final String SCISSORS = "Scissors";
    private static JButton rock;
    private static JButton paper;
    private static JButton scissors;

    public Main() {
        super("Simple Game :D");
        panel = new Container();
        getContentPane().add(panel);

        userScore = new JLabel();
        userScore.setText("Your Score " + getUserCounter());
        userScore.setBounds(300, 50, 100, 40);

        pcScore = new JLabel();
        pcScore.setText("Pc Score " + getPcCounter());
        pcScore.setBounds(430, 50, 100, 40);

        update = new JLabel(Default);
        update.setBounds(325, 0, 400, 40);
        update.enableInputMethods(false);
        update.setBackground(Color.BLUE.brighter());

        panel.add(update);
        panel.add(Rock());
        panel.add(Paper());
        panel.add(Scissors());

        panel.add(userScore);
        panel.add(pcScore);

    }

    public Component Rock() {
        rock = new JButton("Rock");
        rock.setBounds(350, 100, bwidth, bheight);
        rock.setBackground(Color.gray.brighter());
        rock.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Game(ROCK);
            }
        });
        return rock;
    }

    public Component Paper() {
        paper = new JButton("Paper");
        paper.setBounds(350, 130, bwidth, bheight);
        paper.setBackground(Color.WHITE);
        paper.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Game(PAPER);
            }
        });
        return paper;
    }

    public Component Scissors() {
        scissors = new JButton("scissors");
        scissors.setBounds(350, 160, bwidth, bheight);
        scissors.setBackground(new Color(174, 166, 166));
        scissors.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Game(SCISSORS);
            }
        });
        return scissors;
    }

    public String Pc() {
        String pc = "";
        random = new Random();
        int choice = 1 + random.nextInt(3);
        if (choice == 1) {
            pc = ROCK;
        } else if (choice == 2) {
            pc = PAPER;
        } else if (choice == 3) {
            pc = SCISSORS;
        }
        return pc;
    }

    public void Game(String user) {
        String pc = Pc();
        int winner = 0;
        if (user == ROCK && pc == ROCK) {
            update.setText(user + " vs " + pc + " Its a Draw!!");
        } else if (user == ROCK && pc == PAPER) {
            setPcCounter(1);
            update.setText(user + " vs " + pc + " Pc Wins!! ");

        } else if (user == ROCK && pc == SCISSORS) {
            setUserCounter(1);
            update.setText(user + " vs " + pc + " You Win!!");

            // PAPER
        } else if (user == PAPER && pc == PAPER) {
            update.setText(user + " vs " + pc + " Its a Draw!!");
        } else if (user == PAPER && pc == ROCK) {
            update.setText(user + " vs " + pc + " You Win!!");
            userCounter += 1;
        } else if (user == PAPER && pc == SCISSORS) {
            update.setText(user + " vs " + pc + " Pc Wins!!");
            pcCounter += 1;
            // SCISSORS
        } else if (user == SCISSORS && pc == SCISSORS) {
            update.setText(user + " vs " + pc + " Its a Draw!!");
        } else if (user == SCISSORS && pc == ROCK) {
            update.setText(user + " vs " + pc + " Pc Wins!!");
            pcCounter += 1;
        } else if (user == SCISSORS && pc == PAPER) {
            update.setText(user + " vs " + pc + " You Win!!");
            userCounter += 1;
        } else if (user == null || pc == null) {
            System.exit(0);
        }

    }
}

1 个答案:

答案 0 :(得分:1)

中有错误
public static void setUserCounter(int userCounter) {
    Main.userCounter = userCounter + userCounter;
}

应该是

Main.userCounter += userCounter;

Main.userCounter = Main.userCounter + userCounter;

同样的事情:

public static void setPcCounter(int pcCounter) {
    Main.pcCounter = pcCounter + pcCounter;
}

同样在底部的某些地方,你直接使用userCounter变量而不使用setUserCounter();

} else if (user == SCISSORS && pc == PAPER) {
update.setText(user + " vs " + pc + " You Win!!");
userCounter += 1;

保持您的风格一致并使用setUserCounter();

请注意,在对象中,当存在myVar Object字段和myVar参数时,可以通过键入“this.myVar”来将对象字段与“myVar”参数区分开来访问Object字段