java编程训练_JAVA编程训练——Game:scissor, rock, paper

Write a program that plays the popular scissor-rock-paper game. The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and display a message indicating whether the user or the computer wins, loses, or draws. Revise the program to let the user continuously play until either the user or the computer wins more than two times.

import javax.swing.JOptionPane;

import java.util.Scanner;

public class projrct

{

public static void main(String[] args)

{

int computer, you, win_of_computer = 0, win_of_you = 0;

Scanner input = new Scanner(System.in);

int option = JOptionPane.YES_OPTION;

// Game start

while (option == JOptionPane.YES_OPTION)

{

win_of_computer = 0;

win_of_you = 0;

while(win_of_computer-win_of_you!=2&&win_of_you- win_of_computer!=2)

{

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

System.out.print("scissor(0),rock(1),paper(2):");

you = input.nextInt();

if (computer == 0)

{

if (you == 0)

System.out.println("The computer is scissor. You are scissor too. It is a draw");

else if (you == 1)

{

System.out.println("The computer is scissor. You are rock. You won");

win_of_you++;

}

else

{

System.out.println("The computer is scissor. You are paper. You lose");

win_of_computer++;

}

}

else if (computer == 1)

{

if (you == 0)

{

System.out.println("The computer is rock. You are scissor. You lose");

win_of_computer++;

}

else if (you == 1)

System.out.println("The computer is rock. You are rock too. It is a draw");

else

{

System.out.println("The computer is rock. You are paper. You won");

win_of_you++;

}

}

else

{

if (you == 0)

{

System.out.println("The computer is paper. You are scissor. You won");

win_of_you++;

}

else if (you == 1)

{

System.out.println("The computer is paper. You are rock. You lose");

win_of_computer++;

}

else

System.out.println("The computer is paper. You are paper too. It is a draw");

}

}

// print game result

if (win_of_computer > win_of_you)

System.out.println("The computer won finally");

else

System.out.println("You won finally");

option = JOptionPane.showConfirmDialog(null, "Do you want play again?");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值