用java编一个程序,实现两个人玩“石头、剪刀、布”,要求用枚举类型定义石头、剪刀、布
展开全部
public class Test {
public enum Hand{//猜拳枚举
ROCK,SCISSORS,PAPER;
public static Hand getHand(int index){
Hand hand = null;
switch (index) {
case 0:
hand = Hand.ROCK;
break;
case 1:
hand = Hand.SCISSORS;
break;
case 2:
hand = Hand.PAPER;
break;
default:
hand = Hand.ROCK;
break;
}
return hand;
}
}
public static void main(String[] args) {
//两个人猜拳5次
for (int i = 0; i < 5; i++) {
Hand hand1 = Hand.getHand((int)(Math.random()*3));
Hand hand2 = Hand.getHand((int)(Math.random()*3));
judge(hand1, hand2);
}
}
private static void judge(Hand hand1,Hand hand2){
if(hand1 == Hand.ROCK){
if(hand2 == Hand.ROCK){
System.out.println("第一个出拳头,第二个出拳头,平局");
}else if(hand2 == Hand.SCISSORS){
System.out.println("第一个出拳头,第二个出剪刀,第一个赢");
}else if(hand2 == Hand.PAPER){
System.out.println("第一个出拳头,第二个出布,第二个赢");
}
}else if(hand1 == Hand.SCISSORS){
if(hand2 == Hand.ROCK){
System.out.println("第一个出剪刀,第二个出拳头,第二个赢");
}else if(hand2 == Hand.SCISSORS){
System.out.println("第一个出剪刀,第二个出剪刀,平局");
}else if(hand2 == Hand.PAPER){
System.out.println("第一个出剪刀,第二个出布,第一个赢");
}
}else if(hand1 == Hand.PAPER){
if(hand2 == Hand.ROCK){
System.out.println("第一个出布,第二个出拳头,第一个赢");
}else if(hand2 == Hand.SCISSORS){
System.out.println("第一个出布,第二个出剪刀,第二个赢");
}else if(hand2 == Hand.PAPER){
System.out.println("第一个出布,第二个出布,平局");
}
}
}
}
应该不是最优解决办法,先这么着把
更多追问追答
追问
大神谢谢啦
可是得是从键盘输入拳头
展开全部
package TestApp;
import java.awt.EventQueue;
public class testApp {
enum Signal {
Stones, Scissors, Cloth
}
public class TrafficLight {
Signal play = Signal.Cloth;
public String willWin() {
switch (play) {
case Cloth:
play = Signal.Stones;
break;
case Scissors:
play = Signal.Cloth;
break;
case Stones:
play = Signal.Scissors;
break;
}
return play.toString();
}
}
private JFrame frame;
String player1;
String player2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
testApp window = new testApp();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public testApp() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 310);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpringLayout springLayout = new SpringLayout();
frame.getContentPane().setLayout(springLayout);
JLabel lblPlayer = new JLabel("player1:");
springLayout.putConstraint(SpringLayout.NORTH, lblPlayer, 70, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, lblPlayer, 28, SpringLayout.WEST, frame.getContentPane());
frame.getContentPane().add(lblPlayer);
final JButton btnNewButton = new JButton("Stones");
springLayout.putConstraint(SpringLayout.NORTH, btnNewButton, 6, SpringLayout.SOUTH, lblPlayer);
springLayout.putConstraint(SpringLayout.EAST, btnNewButton, 0, SpringLayout.EAST, lblPlayer);
frame.getContentPane().add(btnNewButton);
final JButton btnNewButton_1 = new JButton("Scissors");
springLayout.putConstraint(SpringLayout.SOUTH, btnNewButton_1, -119, SpringLayout.SOUTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, btnNewButton_1, 0, SpringLayout.EAST, lblPlayer);
frame.getContentPane().add(btnNewButton_1);
final JButton btnStones = new JButton("Stones");
springLayout.putConstraint(SpringLayout.EAST, btnStones, -116, SpringLayout.EAST, frame.getContentPane());
frame.getContentPane().add(btnStones);
final JButton btnScissors = new JButton("Scissors");
springLayout.putConstraint(SpringLayout.NORTH, btnScissors, 0, SpringLayout.NORTH, btnNewButton_1);
springLayout.putConstraint(SpringLayout.EAST, btnScissors, 0, SpringLayout.EAST, btnStones);
frame.getContentPane().add(btnScissors);
final JButton btnCloth_1 = new JButton("Cloth");
springLayout.putConstraint(SpringLayout.NORTH, btnCloth_1, 6, SpringLayout.SOUTH, btnScissors);
springLayout.putConstraint(SpringLayout.WEST, btnCloth_1, 0, SpringLayout.WEST, btnStones);
frame.getContentPane().add(btnCloth_1);
JLabel lblPlayer_1 = new JLabel("player2\uFF1A");
springLayout.putConstraint(SpringLayout.NORTH, btnStones, 6, SpringLayout.SOUTH, lblPlayer_1);
springLayout.putConstraint(SpringLayout.NORTH, lblPlayer_1, 0, SpringLayout.NORTH, lblPlayer);
springLayout.putConstraint(SpringLayout.EAST, lblPlayer_1, 0, SpringLayout.EAST, btnStones);
frame.getContentPane().add(lblPlayer_1);
final JButton btnPlay = new JButton("showResult");
springLayout.putConstraint(SpringLayout.WEST, btnPlay, 126, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, btnPlay, -60, SpringLayout.SOUTH, frame.getContentPane());
frame.getContentPane().add(btnPlay);
btnPlay.setVisible(false);
final JButton btnCloth = new JButton("Cloth");
springLayout.putConstraint(SpringLayout.NORTH, btnCloth, 6, SpringLayout.SOUTH, btnNewButton_1);
springLayout.putConstraint(SpringLayout.WEST, btnCloth, 0, SpringLayout.WEST, lblPlayer);
frame.getContentPane().add(btnCloth);
final JLabel lblResult = new JLabel("result:");
springLayout.putConstraint(SpringLayout.NORTH, lblResult, 18, SpringLayout.SOUTH, btnPlay);
springLayout.putConstraint(SpringLayout.WEST, lblResult, 147, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, lblResult, 0, SpringLayout.EAST, btnStones);
frame.getContentPane().add(lblResult);
JButton btnPlay_1 = new JButton("play");
springLayout.putConstraint(SpringLayout.NORTH, btnPlay_1, 28, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, btnPlay_1, 0, SpringLayout.WEST, btnPlay);
frame.getContentPane().add(btnPlay_1);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnNewButton.setVisible(false);
btnNewButton_1.setVisible(false);
btnCloth.setVisible(false);
player1 = btnNewButton.getText();
if (player2 != null) {
btnPlay.setVisible(true);
}
}
});
btnCloth.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnNewButton.setVisible(false);
btnNewButton_1.setVisible(false);
btnCloth.setVisible(false);
player1 = btnCloth.getText();
if (player2 != null) {
btnPlay.setVisible(true);
}
}
});
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnNewButton.setVisible(false);
btnNewButton_1.setVisible(false);
btnCloth.setVisible(false);
player1 = btnNewButton_1.getText();
if (player2 != null) {
btnPlay.setVisible(true);
}
}
});
btnStones.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnStones.setVisible(false);
btnScissors.setVisible(false);
btnCloth_1.setVisible(false);
player2 = btnStones.getText();
if (player1 != null) {
btnPlay.setVisible(true);
}
}
});
btnScissors.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnStones.setVisible(false);
btnScissors.setVisible(false);
btnCloth_1.setVisible(false);
player2 = btnScissors.getText();
if (player1 != null) {
btnPlay.setVisible(true);
}
}
});
btnCloth_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnStones.setVisible(false);
btnScissors.setVisible(false);
btnCloth_1.setVisible(false);
player2 = btnCloth_1.getText();
if (player1 != null) {
btnPlay.setVisible(true);
}
}
});
btnPlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
TrafficLight trafficLight = new TrafficLight();
if(player1.equals("Stones")){
trafficLight.play = Signal.Stones;
}else if(player1.equals("Scissors")){
trafficLight.play = Signal.Scissors;
}else{
trafficLight.play = Signal.Cloth;
}
String win = trafficLight.willWin();
System.out.println(win);
if(player2.equals(win)){
lblResult.setText("result:player1 win");
}else if(player2.equals(player1)){
lblResult.setText("result:Tie");
}else{
lblResult.setText("result:player2 win");
}
}
});
btnPlay_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnNewButton.setVisible(true);
btnNewButton_1.setVisible(true);
btnCloth.setVisible(true);
btnStones.setVisible(true);
btnScissors.setVisible(true);
btnCloth_1.setVisible(true);
btnPlay.setVisible(false);
player1 = null;
player2 = null;
lblResult.setText("result:");
}
});
}
}
import java.awt.EventQueue;
public class testApp {
enum Signal {
Stones, Scissors, Cloth
}
public class TrafficLight {
Signal play = Signal.Cloth;
public String willWin() {
switch (play) {
case Cloth:
play = Signal.Stones;
break;
case Scissors:
play = Signal.Cloth;
break;
case Stones:
play = Signal.Scissors;
break;
}
return play.toString();
}
}
private JFrame frame;
String player1;
String player2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
testApp window = new testApp();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public testApp() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 310);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpringLayout springLayout = new SpringLayout();
frame.getContentPane().setLayout(springLayout);
JLabel lblPlayer = new JLabel("player1:");
springLayout.putConstraint(SpringLayout.NORTH, lblPlayer, 70, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, lblPlayer, 28, SpringLayout.WEST, frame.getContentPane());
frame.getContentPane().add(lblPlayer);
final JButton btnNewButton = new JButton("Stones");
springLayout.putConstraint(SpringLayout.NORTH, btnNewButton, 6, SpringLayout.SOUTH, lblPlayer);
springLayout.putConstraint(SpringLayout.EAST, btnNewButton, 0, SpringLayout.EAST, lblPlayer);
frame.getContentPane().add(btnNewButton);
final JButton btnNewButton_1 = new JButton("Scissors");
springLayout.putConstraint(SpringLayout.SOUTH, btnNewButton_1, -119, SpringLayout.SOUTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, btnNewButton_1, 0, SpringLayout.EAST, lblPlayer);
frame.getContentPane().add(btnNewButton_1);
final JButton btnStones = new JButton("Stones");
springLayout.putConstraint(SpringLayout.EAST, btnStones, -116, SpringLayout.EAST, frame.getContentPane());
frame.getContentPane().add(btnStones);
final JButton btnScissors = new JButton("Scissors");
springLayout.putConstraint(SpringLayout.NORTH, btnScissors, 0, SpringLayout.NORTH, btnNewButton_1);
springLayout.putConstraint(SpringLayout.EAST, btnScissors, 0, SpringLayout.EAST, btnStones);
frame.getContentPane().add(btnScissors);
final JButton btnCloth_1 = new JButton("Cloth");
springLayout.putConstraint(SpringLayout.NORTH, btnCloth_1, 6, SpringLayout.SOUTH, btnScissors);
springLayout.putConstraint(SpringLayout.WEST, btnCloth_1, 0, SpringLayout.WEST, btnStones);
frame.getContentPane().add(btnCloth_1);
JLabel lblPlayer_1 = new JLabel("player2\uFF1A");
springLayout.putConstraint(SpringLayout.NORTH, btnStones, 6, SpringLayout.SOUTH, lblPlayer_1);
springLayout.putConstraint(SpringLayout.NORTH, lblPlayer_1, 0, SpringLayout.NORTH, lblPlayer);
springLayout.putConstraint(SpringLayout.EAST, lblPlayer_1, 0, SpringLayout.EAST, btnStones);
frame.getContentPane().add(lblPlayer_1);
final JButton btnPlay = new JButton("showResult");
springLayout.putConstraint(SpringLayout.WEST, btnPlay, 126, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, btnPlay, -60, SpringLayout.SOUTH, frame.getContentPane());
frame.getContentPane().add(btnPlay);
btnPlay.setVisible(false);
final JButton btnCloth = new JButton("Cloth");
springLayout.putConstraint(SpringLayout.NORTH, btnCloth, 6, SpringLayout.SOUTH, btnNewButton_1);
springLayout.putConstraint(SpringLayout.WEST, btnCloth, 0, SpringLayout.WEST, lblPlayer);
frame.getContentPane().add(btnCloth);
final JLabel lblResult = new JLabel("result:");
springLayout.putConstraint(SpringLayout.NORTH, lblResult, 18, SpringLayout.SOUTH, btnPlay);
springLayout.putConstraint(SpringLayout.WEST, lblResult, 147, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, lblResult, 0, SpringLayout.EAST, btnStones);
frame.getContentPane().add(lblResult);
JButton btnPlay_1 = new JButton("play");
springLayout.putConstraint(SpringLayout.NORTH, btnPlay_1, 28, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, btnPlay_1, 0, SpringLayout.WEST, btnPlay);
frame.getContentPane().add(btnPlay_1);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnNewButton.setVisible(false);
btnNewButton_1.setVisible(false);
btnCloth.setVisible(false);
player1 = btnNewButton.getText();
if (player2 != null) {
btnPlay.setVisible(true);
}
}
});
btnCloth.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnNewButton.setVisible(false);
btnNewButton_1.setVisible(false);
btnCloth.setVisible(false);
player1 = btnCloth.getText();
if (player2 != null) {
btnPlay.setVisible(true);
}
}
});
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnNewButton.setVisible(false);
btnNewButton_1.setVisible(false);
btnCloth.setVisible(false);
player1 = btnNewButton_1.getText();
if (player2 != null) {
btnPlay.setVisible(true);
}
}
});
btnStones.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnStones.setVisible(false);
btnScissors.setVisible(false);
btnCloth_1.setVisible(false);
player2 = btnStones.getText();
if (player1 != null) {
btnPlay.setVisible(true);
}
}
});
btnScissors.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnStones.setVisible(false);
btnScissors.setVisible(false);
btnCloth_1.setVisible(false);
player2 = btnScissors.getText();
if (player1 != null) {
btnPlay.setVisible(true);
}
}
});
btnCloth_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnStones.setVisible(false);
btnScissors.setVisible(false);
btnCloth_1.setVisible(false);
player2 = btnCloth_1.getText();
if (player1 != null) {
btnPlay.setVisible(true);
}
}
});
btnPlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
TrafficLight trafficLight = new TrafficLight();
if(player1.equals("Stones")){
trafficLight.play = Signal.Stones;
}else if(player1.equals("Scissors")){
trafficLight.play = Signal.Scissors;
}else{
trafficLight.play = Signal.Cloth;
}
String win = trafficLight.willWin();
System.out.println(win);
if(player2.equals(win)){
lblResult.setText("result:player1 win");
}else if(player2.equals(player1)){
lblResult.setText("result:Tie");
}else{
lblResult.setText("result:player2 win");
}
}
});
btnPlay_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnNewButton.setVisible(true);
btnNewButton_1.setVisible(true);
btnCloth.setVisible(true);
btnStones.setVisible(true);
btnScissors.setVisible(true);
btnCloth_1.setVisible(true);
btnPlay.setVisible(false);
player1 = null;
player2 = null;
lblResult.setText("result:");
}
});
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Scanner;
import java.util.Random;
public class youxi{
public static void main(String[] args) {
a();
}
public static void a(){
int p=0;
for(int c=1;c<=3;c++){
Scanner d=new Scanner(System.in);
System.out.println("shuru");
int b=d.nextInt();
Random e=new Random();
int f=e.nextInt(2);
System.out.println(f);
if(b==f){
p=p+0;
}
if(b==0){
if(f==1){
p=p-1;
}
else if(f==2){
p=p+1;
}
}
if(b==1){
if(f==0){
p=p+1;
}
if(f==2){
p=p-1;
}
}
if(b==2){
if(f==0){
p=p-1;
}
if(f==1){
p=p+1;
}
}
}
if(p==0){
System.out.println("heju");
}else if(p>0){
System.out.println("renyin");
}else
System.out.println("jiqiy");
}
}
import java.util.Random;
public class youxi{
public static void main(String[] args) {
a();
}
public static void a(){
int p=0;
for(int c=1;c<=3;c++){
Scanner d=new Scanner(System.in);
System.out.println("shuru");
int b=d.nextInt();
Random e=new Random();
int f=e.nextInt(2);
System.out.println(f);
if(b==f){
p=p+0;
}
if(b==0){
if(f==1){
p=p-1;
}
else if(f==2){
p=p+1;
}
}
if(b==1){
if(f==0){
p=p+1;
}
if(f==2){
p=p-1;
}
}
if(b==2){
if(f==0){
p=p-1;
}
if(f==1){
p=p+1;
}
}
}
if(p==0){
System.out.println("heju");
}else if(p>0){
System.out.println("renyin");
}else
System.out.println("jiqiy");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询