java基础猜拳游戏

import java.util.*;
public class Person {    //玩家类
	String name="";    //用户名字
	int score =0;   //初始值积分
	String[] box= {"剪刀","石头","布"};  //用数组来录取这三个值
	public int showFist() {    //用int返回值方便一点
		Scanner input=new Scanner(System.in);
		System.out.print("请输入:1.剪刀2.石头3布(输入相应的数):");
		int index=input.nextInt();
		System.out.println(name+"出拳:"+box[index-1]);//因为数组的下标是从0开始,所以当你输                        
                                                       入1时根据提示对应是剪刀,所以index-1
		return  index;	//返回index,让Game()方法接收这个数值
	}
}



public class Computer {    //电脑类
	String name="";
	int score=0;
	String[] box= {"剪刀","石头","布"};
	public int Stat() {
		int random=(int)(Math.random()*3);
		System.out.println(name+"出拳:"+box[random]);
		return random+1;
	}
}

import java.util.*;
public class Game {    //游戏类
	String owner="";
	String sthyl="";
	int conut=0;
	String q="";
	String[] flgure={"唐文涛","李晨","李雨婷"};
	public void show() {
		Scanner input=new Scanner(System.in);
		System.out.println("请输入你的名字:");
		String name=input.next();
		System.out.println("请选择你的对手:1.唐文涛2.李晨3.李雨婷");
		int com=input.nextInt();
		Computer Com=new Computer();
		Person yh=new Person();
		yh.name=name;	//前面是Person里面的name,后面是你刚才输入的名字
		Com.name=flgure[com-1];
		owner=yh.name;
		sthyl=Com.name;
		System.out.println("游戏1规则:胜利加1分");
		System.out.println("\t甲方玩家:"+owner);
		System.out.println("\t乙方玩家:"+sthyl);
		System.out.println("\t\t"+owner+"Vs"+sthyl);
		do {
		int a=yh.showFist();    //int a是接收数值,yh.showFist();是调用方法,所以在接收数值                            
                                  的同时,也调用了方法里面的东西
		int b=Com.Stat();
		if(a==b) {//无参方法
			System.out.println("平局");
		}else if(a==1&&b==3||a==2&&b==1||a==3&&b==2) {
		System.out.println("恭喜玩家"+owner+"胜利");
		yh.score++;    //如果owner胜利,就加1
		}else if(b==1&&a==3||b==2&&a==1||b==3&&a==2){
			System.out.println("恭喜玩家"+sthyl+"胜利");
			Com.score++;//如果sthyl胜利,就加1
		}
		System.out.println("下一把嘛y/n");
		 q=input.next();
		}while(q.equals("y"));
		System.out.println("对战次数:"+conut);
		System.out.println("姓名:\t"+"积分");
		System.out.println(owner+":"+yh.score);
		System.out.println(sthyl+":"+Com.score);
		
	}
}

public class Test {	//测试类

	public static void main(String[] args) {    //main方法不要写逻辑代码,行业规定
		// TODO Auto-generated method stub
		Game nn=new Game();
		nn.show();
		
	}

}

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是用Java实现的猜拳游戏示例代码: ```java import java.util.Random; import java.util.Scanner; public class RockPaperScissors { public static void main(String[] args) { Scanner input = new Scanner(System.in); Random random = new Random(); String[] options = {"石头", "剪刀", "布"}; int computerScore = 0; int playerScore = 0; System.out.println("欢迎来到猜拳游戏!"); while (true) { System.out.println("\n请选择您要出的手势:"); System.out.println("1. 石头"); System.out.println("2. 剪刀"); System.out.println("3. 布"); System.out.print("请输入数字选择手势(输入 0 结束游戏):"); int playerChoice = input.nextInt(); if (playerChoice == 0) { break; } if (playerChoice < 1 || playerChoice > 3) { System.out.println("输入无效,请重新输入!"); continue; } int computerChoice = random.nextInt(3) + 1; System.out.println("电脑出了:" + options[computerChoice - 1]); if (playerChoice == computerChoice) { System.out.println("平局!"); } else if ((playerChoice == 1 && computerChoice == 2) || (playerChoice == 2 && computerChoice == 3) || (playerChoice == 3 && computerChoice == 1)) { System.out.println("恭喜您获胜!"); playerScore++; } else { System.out.println("很遗憾,您输了!"); computerScore++; } System.out.println("您的得分:" + playerScore); System.out.println("电脑的得分:" + computerScore); } System.out.println("游戏结束,谢谢您的参与!"); } } ``` 这个程序使用了while循环,玩家可以选择石头、剪刀或布来进行游戏,电脑会随机出手势。程序会根据出拳的结果判断胜负,并输出得分。当玩家输入 0 时,程序会结束游戏。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值