交作业喽~~~~ 主文件 import java.io.*; public class caiquanMAIN { public static void main(String[] args)throws Exception { int C=1; CPUAI cpu=new CPUAI(); System.out.println("1.剪刀;2.石头;3.布;请选择:"); InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); do { C=br.read(); }while (C>0 &&C<4); br.close(); System.out.println(cpu.chuquan()); switch (cpu.jieguo(C)) { case 0: System.out.println("平了"); break; case 1: System.out.println("你赢了"); break; case 2: System.out.println("你输了"); break; default:
} } } 另一个文件 import java.sql.Date; import java.util.Random; public class CPUAI { private int chu=0; private String[] caiquan; public CPUAI() { chu=0;//准备状态 caiquan=new String[3]; caiquan[0]=new String("石头"); caiquan[1]=new String("剪刀"); caiquan[2]=new String("布"); } public String chuquan() { Random a=new Random(); do { chu=a.nextInt(10); }while(chu<=0|chu>=4); //System.out.println("chu"+chu);
return caiquan[chu-1]; } public int jieguo(int a) { int x=0; //System.out.println("a"+a); switch (a) { case 50://石头 switch (chu) { case 1://石头 x=0;//平局 break; case 2://剪刀 x=1;//输了 break; case 3://布 x=2;//赢了 break; default: break;
} break; case 49://剪刀 switch (chu) { case 1://石头 x=2;//赢了 break; case 2://剪刀 x=0;//平局 break; case 3://布 x=1;//输了 break; default: break;
} break; case 51://布 switch (chu) { case 1://石头 x= 1;//输了 break; case 2://剪刀 x= 2;//赢了 break; case 3://布 x= 0;//平局 break; default: break;
} break; default: break;
} return x; } }偶刚弄出来····呵呵,,学了java 3天······
楼上你真厉害 我也是刚学到这 呵呵 我写完了 呵呵 分为4个文件:这个是用户出拳的类文件: package com.game.guess; import java.util.Scanner; public class Person { String name = "匿名"; int score = 0;
public int showFist(){ Scanner input = new Scanner(System.in); System.out.print("\n请出拳:1.剪刀 2.石头 3.布 (输入相应数字) :"); int show = input.nextInt(); switch(show){ case 1: System.out.println("你出拳: 剪刀"); break; case 2: System.out.println("你出拳: 石头"); break; case 3: System.out.println("你出拳: 布"); break; } return show; } } 这个是电脑出拳的类文件: package com.game.guess;public class Computer { String name = "匿名"; int score = 0;;
public int showFist(){ int show = (int)(Math.random()*10)%3 + 1; //产生随机数,表示电脑出拳 switch(show){ case 1: System.out.println("电脑出拳: 剪刀"); break; case 2: System.out.println("电脑出拳: 石头"); break; case 3: System.out.println("电脑出拳: 布"); break; } return show; } }这个是游戏类的文件:package com.game.guess; import java.util.Scanner; public class Game { Person person; //甲方 Computer computer; //乙方 int count; //对战次数
/** * 初始化 */ public void initial(){ person = new Person(); computer = new Computer(); count = 0; }
主文件
import java.io.*;
public class caiquanMAIN {
public static void main(String[] args)throws Exception
{
int C=1;
CPUAI cpu=new CPUAI();
System.out.println("1.剪刀;2.石头;3.布;请选择:");
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
do
{
C=br.read();
}while (C>0 &&C<4);
br.close();
System.out.println(cpu.chuquan());
switch (cpu.jieguo(C))
{
case 0:
System.out.println("平了");
break;
case 1:
System.out.println("你赢了");
break;
case 2:
System.out.println("你输了");
break;
default:
}
}
}
另一个文件
import java.sql.Date;
import java.util.Random;
public class CPUAI {
private int chu=0;
private String[] caiquan;
public CPUAI()
{
chu=0;//准备状态
caiquan=new String[3];
caiquan[0]=new String("石头");
caiquan[1]=new String("剪刀");
caiquan[2]=new String("布");
}
public String chuquan()
{
Random a=new Random();
do
{
chu=a.nextInt(10);
}while(chu<=0|chu>=4);
//System.out.println("chu"+chu);
return caiquan[chu-1];
}
public int jieguo(int a)
{ int x=0;
//System.out.println("a"+a);
switch (a)
{
case 50://石头
switch (chu)
{
case 1://石头
x=0;//平局
break;
case 2://剪刀
x=1;//输了
break;
case 3://布
x=2;//赢了
break;
default:
break;
}
break;
case 49://剪刀
switch (chu)
{
case 1://石头
x=2;//赢了
break;
case 2://剪刀
x=0;//平局
break;
case 3://布
x=1;//输了
break;
default:
break;
}
break;
case 51://布
switch (chu)
{
case 1://石头
x= 1;//输了
break;
case 2://剪刀
x= 2;//赢了
break;
case 3://布
x= 0;//平局
break;
default:
break;
}
break;
default:
break;
}
return x;
}
}偶刚弄出来····呵呵,,学了java 3天······
package com.game.guess;
import java.util.Scanner;
public class Person {
String name = "匿名";
int score = 0;
public int showFist(){
Scanner input = new Scanner(System.in);
System.out.print("\n请出拳:1.剪刀 2.石头 3.布 (输入相应数字) :");
int show = input.nextInt();
switch(show){
case 1:
System.out.println("你出拳: 剪刀");
break;
case 2:
System.out.println("你出拳: 石头");
break;
case 3:
System.out.println("你出拳: 布");
break;
}
return show;
}
}
这个是电脑出拳的类文件:
package com.game.guess;public class Computer {
String name = "匿名";
int score = 0;;
public int showFist(){
int show = (int)(Math.random()*10)%3 + 1; //产生随机数,表示电脑出拳
switch(show){
case 1:
System.out.println("电脑出拳: 剪刀");
break;
case 2:
System.out.println("电脑出拳: 石头");
break;
case 3:
System.out.println("电脑出拳: 布");
break;
}
return show;
}
}这个是游戏类的文件:package com.game.guess;
import java.util.Scanner;
public class Game {
Person person; //甲方
Computer computer; //乙方
int count; //对战次数
/**
* 初始化
*/
public void initial(){
person = new Person();
computer = new Computer();
count = 0;
}
/**
* 开始游戏
*/
public void startGame() {
System.out.println("----------------欢 迎 进 入 游 戏 世 界----------------\n");
System.out.println("\n\t\t******************");
System.out.println ("\t\t** 猜拳, 开始 **");
System.out.println ("\t\t******************");
System.out.println("\n\n出拳规则:1.剪刀 2.石头 3.布");
/*选择对方角色*/
System.out.print("请选择角色(1:刘备 2:孙权 3:曹操): ");
Scanner input = new Scanner(System.in);
int role = input.nextInt();
if(role == 1){
computer.name = "刘备";
}else if(role == 2){
computer.name = "孙权";
}else if(role == 3){
computer.name = "曹操";
}
System.out.print("\n要开始吗?(y/n) ");
String con = input.next();
int perFist; //用户出的拳
int compFist; //计算机出的拳
while(con.equals("y")){
/*出拳*/
perFist = person.showFist();
compFist = computer.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("结果: 恭喜, 你赢了!"); //用户赢
person.score++;
}else{
System.out.println("结果说:^_^,你输了,真笨!\n"); //计算机赢
computer.score++;
}
count++;
System.out.print("\n是否开始下一轮(y/n): ");
con = input.next();
}
/*显示结果*/
showResult();
}
/**
* 显示比赛结果
*/
public void showResult(){
/*显示最后结果*/
System.out.println("---------------------------------------------------");
System.out.println(computer.name + " VS " + person.name);
System.out.println("对战次数:"+ count);
int result = calcResult();
if(result == 1){
System.out.println("结果:打成平手,下次再和你一分高下!");
}else if(result == 2){
System.out.println("结果:恭喜恭喜!"); //用户获胜
}else{
System.out.println("结果:呵呵,笨笨,下次加油啊!"); //计算机获胜
}
System.out.println("---------------------------------------------------");
}
/**
* 计算比赛结果
*/
public int calcResult(){
if(person.score == computer.score){
return 1;
}else if(person.score > computer.score){
return 2;
}else{
return 3;
}
}
}
这个是测试类:
package com.game.guess;public class StartGuess { /**
* 人机互动版猜拳游戏
* @param args
*/
public static void main(String[] args) {
Game game = new Game();
game.initial();
game.startGame();
}
}