十二章作业

package cn.jbit.test;

import java.util.Random;

/**
 * 电脑类
 */
public class Computer {
	public String name;//名字
	public int score;//分数
	/*
	 * 电脑出拳方法
	 */
	public int showFist(){
		Random r=new Random();
		int num=r.nextInt();
		switch(num){
		case 1:
			System.out.println(name+"出的是剪刀");
			break;
		case 2:
			System.out.println(name+"出的是石头");
			break;
		case 3:
			System.out.println(name+"出的是布");
			break;
		}
		return num;
	}
}


package cn.jbit.test;

import java.util.Scanner;

/**
 * 用户类
 */
public class Person {
	public String name;//名字
	public int score;//分数
	/*
	 * 用户出拳方法
	 */
	public int showFist(){
		Scanner input=new Scanner(System.in);
		int ch=0;
		boolean m=true;
		while(m){
			System.out.println("1.剪刀2.石头3.布");
			ch=input.nextInt();
			switch(ch){
			case 1:
				System.out.println("你出的是剪刀");
				m=false;
				break;
			case 2:
				System.out.println("你出的是石头");
				m=false;
				break;
			case 3:
				System.out.println("你出的是布");
				m=false;
				break;
			default:
				System.out.println("输入错误");
				break;
			}
		}
		return ch;
	}
}


package cn.jbit.test;

import java.util.Scanner;

/**
 * 游戏类
 */
public class Game {
	public Person person;//用户玩家
	public Computer computer;//电脑玩家
	public int count;//对战次数
	Scanner input=new Scanner(System.in);
	/*
	 * 初始值方法
	 */
	public void init(){
		person=new Person();
		computer=new Computer();
	}
	/*
	 * 游戏方法
	 */
	public void staterGame(){
		init();
		int p=0;//用户出拳
		int c=0;//电脑出拳
		System.out.println("************");
		System.out.println("**猜拳 ,开始**");
		System.out.println("************");
		System.out.println("出拳规则:1.剪刀2.石头3.布");
		System.out.println("请选择对方角色(1.刘备2.孙权3.曹操):");
		switch(input.nextInt()){
		case 1:
			computer.name="刘备";
			break;
		case 2:
			computer.name="孙权";
			break;
		case 3:
			computer.name="曹操";
			break;
		}
		System.out.println("请输入你的名字:");
		person.name=input.next();
		System.out.println(person.name+"VS"+computer.name);
		System.out.println("是否开始(y/n)");
		char a=input.next().charAt(0);
		if(a=='y'){
			char b='y';
			while(b=='y'){
				System.out.println("请出拳:");
				p=person.showFist();
				c=computer.showFist();
				if(p==c){
					System.out.println("平局");
				}else if(p==1&&c==3||p==2&&c==1||p==3&&c==2){
					System.out.println("恭喜,您赢了");
					person.score+=1;
				}else{
					System.out.println("笨蛋。你输了");
					computer.score+=1;
				}
				count++;
				System.out.println("是否开始下一局?(y/n):");
				b=input.next().charAt(0);
			}
			show();
		}else{
			System.out.println("游戏结束");
		}
	}
	/*
	 * 对战结果方法
	 */
	public void show(){
		System.out.println(person.name+"VS"+computer.name);
		System.out.println("对战次数"+count+"\n");
		System.out.println("姓名\t得分");
		System.out.println(person.name+"\t"+person.score);
		System.out.println(computer.name+"\t"+computer.score);
		if(person.score>computer.score){
			System.out.println("你太棒了,你赢了");
		}else if(person.score<computer.score){
			System.out.println("笨蛋,,你输了");
		}else{
			System.out.println("哈哈哈,,平局");
		}
	}
}


package cn.jbit.test;

public class TestGame {

	/**
	 * 测试类
	 */
	public static void main(String[] args) {
		Game game=new Game();
		game.staterGame();

	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值