「java组合拳」用java编写猜拳游戏
今天给各位分享java组合拳的知识,其中也会对用java编写猜拳游戏进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java 石头剪刀布 class, console和GUI
- 2、求一个java猜拳游戏程序
- 3、求JAVA人机猜拳的代码,类似一下界面。
- 4、用java编写一个猜拳游戏
- 5、java代码猜拳游戏相关代码请教
- 6、急求一个JAVA编程,“编写一个猜拳游戏,分别用0、1、2表示石头、剪子、布。
java 石头剪刀布 class, console和GUI
import java.util.Scanner;
public final class Second {
public static void main(String[] args) {
System.out.println("欢迎进入游戏的世界");
System.out.println("*******************");
System.out.println("**猜拳 开始**");
System.out.println("*******************");
System.out.println("出拳规则。0:剪刀,1:石头,2:布");
for (;;) {
Scanner s = new Scanner(System.in);
int you = s.nextInt();
int pc = (int)(3 * Math.random());
String str="你出"+you+",系统出"+pc;
str.replaceAll("0", "剪刀");//0:剪刀,1:包,2:锤
str.replaceAll("1", "石头");
str.replaceAll("2", "布");
System.out.print(str+": ");
if ((pc + 1) % 3 == you) { //循环制约,经典算法
System.out.println("你输了");
} else if (pc == you) {
System.out.println("平手再来");
} else {
System.out.println("你赢了");
}
}
}
}
求一个java猜拳游戏程序
package test;
import java.util.Random;
import java.util.Scanner;
/**
* 猜拳游戏思路
* 1、定义输入函数
* 2、提示用户输入猜拳数值
* 3、定义随机一个数作为电脑数值
* 4、判断[用户输入数值]与 [电脑随机数值]
* 5、能够相等就是打平,不能相等就利用、||逻辑符判断输赢
* 6、设定数值1-石头 2-剪刀 3-布
*/
public class CaiQuanYouXi {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);//定义输入函数in,Scanner包功能,输入数值用的
System.out.println("--------------猜拳游戏---------------");
System.out.println("请输入一个数值:1、石头 2、剪刀 3、布");//提示输入数值
System.out.println(" ");//空行
int x=in.nextInt();//让用户输入X的数值
Random on=new Random();//定义电脑的随机数值的函数on
int y=on.nextInt(3)+1;//定义y随机函数数值范围(1--3)
if(x=4||x==0){ //判断用户是否输入非1--3范围
System.out.println("亲,请正确输入:1、石头 2、剪刀 3、布。你输入了:"+x);
}else{
/*下面是判断用户输入x的数值 嵌套if*/
if(x==y){
if(x==1){ //判断打平的情况
System.out.println("你:石头------电脑:石头 PK:很幸运打平手");
}else if(x==2){
System.out.println("你:剪刀------电脑:剪刀 PK:很幸运打平手");
}else {
System.out.println("你:布------电脑:布 PK:很幸运打平手");
}
}else if(x==1y==2||x==2y==3||x==3y==1){ //开始判断赢的情况
if(x==1y==2){
System.out.println("你:石头------电脑:剪刀 PK:恭喜您,赢了!");
}else if(x==2y==3){
System.out.println("你:剪刀------电脑:布 PK:恭喜您,赢了!");
}else {
System.out.println("你:布------电脑:石头 PK:恭喜您,赢了!");
}
}else {//开始判断输的情况
if(x==1y==3){
System.out.println("你:石头------电脑:布 PK:很遗憾,输了!");
}else if(x==2y==1){
System.out.println("你:剪刀------电脑:石头 PK:很遗憾,输了!");
}else {
System.out.println("你:布------电脑:剪刀 PK:很遗憾,输了!");
}
}
}
}
}
运行后的效果展示:
--------------猜拳游戏---------------
请输入一个数值:1、石头 2、剪刀 3、布
1
你:石头------电脑:布 PK:很遗憾,输了!
--------------猜拳游戏---------------
请输入一个数值:1、石头 2、剪刀 3、布
4
亲,请正确输入:1、石头 2、剪刀 3、布。你输入了:4
求JAVA人机猜拳的代码,类似一下界面。
自己纯手打,老半天才弄出来啊
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Demo2 extends JFrame {
private JLabel lb1, lb2, lb3, lb4; // 提示标签
private JTextField ta1, ta2;// 两个文本框
private JButton b1, b2, b3; // 三个按钮
private JPanel p1, p2; // 两个JPanel面板
public Demo2() {
// 初始化所有组件
lb1 = new JLabel("欢迎使用人机猜拳程序");
lb2 = new JLabel("你出拳: ");
lb3 = new JLabel("电脑出拳:");
lb4 = new JLabel("结果");
ta1 = new JTextField();
ta1.setPreferredSize(new Dimension(60, 60)); // 设置大小
ta1.setEditable(false);//设置不可编辑
ta2 = new JTextField();
ta2.setPreferredSize(new Dimension(60, 60));
ta2.setEditable(false);//设置不可编辑
b1 = new JButton("剪刀");
b2 = new JButton("石头");
b3 = new JButton("布");
p1 = new JPanel();
p2 = new JPanel();
// 设置第一个面板内容
Box box = Box.createVerticalBox();
Box box1 = Box.createHorizontalBox();
box1.add(lb2);
box1.add(ta1);
box1.add(lb3);
box1.add(ta2);
box.add(lb1);
box.add(Box.createVerticalStrut(40));
box.add(box1);
box.add(Box.createVerticalStrut(10));
box.add(lb4);
box.add(new JLabel());
p1.add(box);
// 设置第二个面板
p2.setLayout(new GridBagLayout()); // 使用GridBagLayout布局管理器
p2.setPreferredSize(new Dimension(0, 60));
GridBagConstraints g2 = new GridBagConstraints();
g2.fill = GridBagConstraints.BOTH;
g2.weightx = 1.0;
g2.weighty = 1.0;
g2.gridx = 0;
g2.gridy = 0;
p2.add(b1, g2);
g2.gridx = 1;
p2.add(b2, g2);
g2.gridx = 2;
p2.add(b3, g2);
//为3个按钮添加事件
b1.addActionListener(new buttonAction());
b2.addActionListener(new buttonAction());
b3.addActionListener(new buttonAction());
this.getContentPane().add(p1);
this.getContentPane().add(p2, BorderLayout.SOUTH);
this.setTitle("机器人猜拳游戏");
this.setSize(300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//事件类
class buttonAction extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
ta1.setText("剪刀");
init(ta1.getText());
}else if(e.getSource()==b2){
ta1.setText("石头");
init(ta1.getText());
}else if(e.getSource()==b3){
ta1.setText("布");
init(ta1.getText());
}
}
// 模拟电脑出拳,产生三个随机数。0代表剪刀,1代表石头,2代表布
public String getQuan(){
String str="";
int num=new Random().nextInt(3) ;
if(num==0){
str="剪刀";
}else if(num==1){
str="石头";
}else if(num==2){
str="布";
}
return str;
}
// 判断输赢方法
public String isying(String s1,String s2){
String s="";
if(s1.equals(s2)){
s="平局";
}else if(s1.equals("剪刀")s2.equals("布")){
s="你赢";
}else if(s1.equals("石头")s2.equals("剪刀")){
s="你赢";
}else if(s1.equals("布")s2.equals("石头")){
s="你赢";
}else{
s="电脑赢";
}
return s;
}
public void init(String wo){
String sy=""; // 保存输赢结果
String dncq=getQuan(); //电脑出拳
if(wo.equals(dncq)){
sy="平局";
}else if(wo.equals("剪刀")dncq.equals("布")){
sy="你赢";
}else if(wo.equals("石头")dncq.equals("剪刀")){
sy="你赢";
}else if(wo.equals("布")dncq.equals("石头")){
sy="你赢";
}else{
sy="电脑赢";
}
ta2.setText(dncq);// 电脑出拳
lb4.setText("结果:"+sy);
}
}
public static void main(String[] args) {
new Demo2();
}
}
用java编写一个猜拳游戏
import java.util.Scanner;
public class Game {
private Scanner scan = new Scanner(System.in);
private String rule[] = {"","剪刀","石头","布"};
private String role[] = {"","刘备","孙权","曹操"};
private Computer computer;
private User user;
private int roundCount;
private Game(){
roundCount = 0;
}
public static void main(String[] args) {
Game game = new Game();
game.start();
}
public void start(){
computer = new Computer();
System.out.println("- - 欢迎进入游戏世界 - -");
System.out.println("** ******************");
System.out.println("** 猜拳,开始 **");
System.out.println("** ******************");
System.out.println();
System.out.println("出拳规则:1.剪刀 2.石头 3.布");
System.out.print("请选择角色(1:刘备 2.孙权 3.曹操)");
int userRole = scan.nextInt();
if(userRole =1 userRole =3){
user = new User(role[userRole]);
System.out.println();
System.out.print("要开始吗?(y/n) ");
String start = scan.next();
if(start.equals("y")){
round();
}
else if(start.equals("n")){
}
}
}
private void round(){
roundCount ++;
System.out.println();
System.out.print("请出拳:1.剪刀 2.石头 3.布 (输入相应数字): ");
int userRule = user.round();
if(userRule = 1 userRule = 3){
int computerRule = computer.round();
judge(userRule,computerRule);
System.out.println();
System.out.print("是否开始下一轮(y/n): ");
String next = scan.next();
if(next.equals("y")){
round();
}
else if(next.equals("n")){
end();
}
}
}
private void judge(int ur,int cr){
System.out.println("你出拳:"+ rule[ur]);
System.out.println("电脑出拳:"+ rule[cr]);
if(ur == cr){
System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!");
}
else if((ur == 1 cr== 3)||
(ur == 2 cr == 1)||
(ur == 3 cr == 2)){
System.out.println("结果:你赢了!");
user.win();
}
else{
System.out.println("结果:你输了!");
computer.win();
}
}
private void end(){
System.out.println("- -------------------------------------");
System.out.println(user.getName() + "VS" + computer.getName());
System.out.println("对战次数:" + roundCount);
if(computer.getWin() == user.getWin()){
System.out.println("结果:打成平手,下次再和你一分高下!");
}
else if(computer.getWin() user.getWin()){
System.out.println("结果:你输了!电脑赢了" + computer.getWin()+"次!");
}
else{
System.out.println("结果:你赢了!你赢了" + user.getWin()+"次!");
}
System.out.println("- -------------------------------------");
}
class Computer{
private int win;
private String name;
public Computer(){
win = 0;
name = "匿名";
}
public int round(){
return (int)(System.currentTimeMillis() % 3) + 1;//随机返回1、2、3
}
public String getName(){
return name;
}
public void win(){
win ++;
}
public int getWin(){
return win;
}
}
class User{
private int win;
private String name = "";
public User(String name){
this.name = name;
win = 0;
}
public int round(){
return scan.nextInt();
}
public String getName(){
return name;
}
public void win(){
win ++;
}
public int getWin(){
return win;
}
}
}
刚刚写好的,应该满足你的题目要求。赢了和输了的提示信息题目里没有是我自己随便写的。
java代码猜拳游戏相关代码请教
comp是电脑产生的随机数字(电脑出的拳),people 是人出的拳。 因为剪刀石头布只有 1 2 3
。如果电脑的数字比的你刚好大1,就是它比你的大。 如21,32对应就是(石头大于剪刀,布大于石头)。 但也有可能是剪刀大于布 。 那么剪刀的位子是1 ,布的位子是3. 所以当电脑数字减你的数字等于2时 就是(电脑出的布 ,你出的石头这样的情况了)。
所以是if((comp-people)==-1||(comp-people)==2) 这两者结合就是你赢的时候
急求一个JAVA编程,“编写一个猜拳游戏,分别用0、1、2表示石头、剪子、布。
用我自己的方法写了一个,看LZ喜欢不喜欢
12是石头,23是剪子,31是布.first表示第一个人。second表示第二个人!
import java.util.Random;
public class Print {
public static void main(String[] args) {
//"12"是石头,“23”是剪子,“31”是布
String[] a = {"12", "23", "31"};
Random r = new Random ();
String first = a[r.nextInt(3)];
String second = a[r.nextInt(3)];
System.out.println("12是石头,23是剪子,31是布.first表示第一个人。second表示第二个人!\n---------");
if (first.equals(second)) {
System.out.println(first + "--" + second + ",(前面是first,后一个是second)" + ":平了");
} else if (first.charAt(1) == second.charAt(0)) {
System.out.println("出拳情况:" + first + "--" + second + ",(前面是first,后一个是second)" + "\n结果判断:" + result1(first,second));
} else if (first.charAt(0) == second.charAt(1)) {
System.out.println("出拳情况:" + first + "--" + second + ",(前面是first,后一个是second)" + "\n结果判断:" + result2(first,second));
}
}
public static String result1 (String f, String s) {
if (f.equals("12") s.equals("23")) {
return "first石头 胜 second剪子";
} else if (f.equals("23") s.equals("31")) {
return "first剪子 胜 second布";
} else {
return "first布 胜 second石头";
}
}
public static String result2 (String f, String s) {
if (f.equals("12") s.equals("31")) {
return "first石头 输 second布";
} else if (f.equals("23") s.equals("12")) {
return "first剪子 输 second石头";
} else {
return "first布 输 second剪子";
}
}
}
关于java组合拳和用java编写猜拳游戏的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-21,除非注明,否则均为
原创文章,转载请注明出处。