java练习--人机猜拳

//计算机类:<pre name="code" class="java">public class Person {
	 String name;
	 int x;

	public int showFist() {
		Scanner input = new Scanner(System.in);
		System.out.println("请出拳:1.剪刀2.石头3.布(输入相应的数字)");
		int x = input.nextInt();
		switch (x) {
		case 1:
			System.out.println("你出拳:剪刀");
			break;
		case 2:
			System.out.println("你出拳:石头");
			break;
		case 3:
			System.out.println("你出拳:布");
			break;
		default:
			System.out.println("输入错误");
			break;
		}
		return x;

	}
}


</pre><pre name="code" class="java">
//用户类:
public class Computer {
	public int sc;
	 String name;

	public int showFist() {
		int s = (int) (Math.random() * 10 % 3 + 1);
		switch (s) {
		case 1:
			System.out.println("电脑出拳:剪刀");
			break;
		case 2:
			System.out.println("电脑出拳:石头");
			break;
		case 3:
			System.out.println("电脑出拳:布");
			break;
		default:
			System.out.println("输入错误.请重新输入:");
		}
		return s;
		
	}
}
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">
//测试类:
<pre name="code" class="java">import java.util.Scanner;

public class Game1 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		Person o = new Person();
		Computer on = new Computer();
		int count = 0;
		Person person;
		Computer computer;
		int sc;
		int num = 0;
		int x;
		int sum = 0;
		String name = "";
		System.out.println("----------------欢迎进入游戏世界---------------------");
		System.out.println("\n");
		System.out.println("\t\t*******************\t\t");
		System.out.println("\t\t****\t猜拳,开始\t***\t\t");
		System.out.println("\t\t*******************\t\t");
		System.out.println("出拳规则:1.剪刀2.石头3.布");
		System.out.println("请选择对方角色(1:刘备2:孙权3:曹操)");
		x = input.nextInt();
		String name1 = "";
		switch (x) {
		case 1:
			name1 = "刘备";
			System.out.println("你选择了刘备对战");
			break;
		case 2:
			name1 = "孙权";
			System.out.println("你选择了孙权对战");
			break;
		case 3:
			name1 = "曹操";
			System.out.println("你选择了曹操对战");
			break;
		}
		System.out.println("请输入你的姓名:");
		String n = input.next();
		System.out.println(n + "VS" + name1);
		String con = "";

		System.out.println("\n要开始码?(y/n)");
		con = input.next();

		while (con.equals("y")) {
			int perFist;
			int compFist;
			perFist = o.showFist();
			compFist = on.showFist();
			if ((perFist == 1 && compFist == 1)
					|| (perFist == 2 && compFist == 2)
					|| (perFist == 3 && compFist == 3)) {
				System.out.println("结果:和局,真衰!\n");

			} else if ((perFist == 1 && compFist == 3)
					|| (perFist == 2 && compFist == 1)
					|| (perFist == 3 && compFist == 2)) {
				System.out.println("结果:恭喜,你赢了!");
				num++;
			} else {
				System.out.println("结果说:^_^,你输了,真笨!\n");

				sum++;
			}
			count++;
			System.out.println("\n是否开始下一轮(y/n):");
			con = input.next();
		}
		System.out.println("------------------------------------------");
		System.out.println(n + "VS" + name1);
		System.out.println("对战次数:" + count);
		System.out.println("姓名\t\t得分");
		System.out.println(n + "\t\t" + num);
		System.out.println(name1 + "\t\t" + sum);
		if (sum < num) {
			System.out.println("结果:恭喜");
		} else if (sum == num) {
			System.out.println("结果:还好,平局");
		} else if (sum > num) {
			System.out.println("结果:呵呵,笨笨,下次加油!");
		}
		System.out.println("------------------------------------------");
	}
}




</pre><pre name="code" class="java">


展开阅读全文

没有更多推荐了,返回首页