来个小游戏,小白学了两个月的成果
刚学了两个月,做了个小游戏(人机猜拳),,
*****************我是分页(主方法)
package com.hai.guessGame;
/
- 人机猜拳游戏
- Time:2017-02-15
*/
import java.util.Scanner;
public class Text {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input=new Scanner(System.in);
People people=new People();//人类
Computer computer=new Computer();//系统
Game game=new Game();//游戏
game.initial();//主菜单
game.startGame(people,computer);//游戏初始化
System.out.println("要开始吗?(y/n)");
String start=input.next();
boolean flag=false;
//开始游戏,
while(!start.equals("n")){
people.showList(game);//人类出拳
System.out.println();
computer.showList(game);//系统出拳
game.showResult(people.answerP,computer.i,game);//结果显示
System.out.println("是否开始下一轮?(y/n)");
start=input.next();
if("n".equals(start)){
flag=true;
}
}
if(flag){
game.result();//结束游戏,显示最后结果
}
}
}
package com.hai.guessGame;
/**
- 人物设置
*/
import java.util.Scanner;
public class People {
Scanner input=new Scanner(System.in);
int answerP; //出拳的下标
/**
* 寻找输入数字对应的拳
* @param answer //下标
* @return 人物名称
*/
public int out(int answer) {
return answer-1;
}
public String id(String nameP) {
return nameP;
}
/**
* 人类出拳
* @param game //游戏类对象
*/
public void showList(Game game) {
// TODO Auto-generated method stub
boolean flag=false;
String arr[]=game.arr;
System.out.print("请出拳:(输入对应数字)");
answerP=input.nextInt();
if(answerP==1||answerP==2||answerP==3)
flag=true;
else{
System.out.println("输入错误!请重新出拳!");
showList(game);
}
if(flag){
System.out.print(game.pName+"出拳:");
System.out.print(arr[out(answerP)]);
}
}
}
我是分页******
/**
- 系统设置
- @author X
/
public class Computer {
int i;
String id[]=new String[]{"刘备","孙权","曹操"};
String name;
/* - 系统人物的名称
- @param n //数组下标
- @return //返回名称
/
public String id(int n) {
// TODO Auto-generated method stub
return id[n-1];
}
/*- 系统出拳
- @param game //游戏类的对象名
/
public void showList(Game game) {
// TODO Auto-generated method stub
String arr[]=game.arr;
System.out.print(game.cName+"出拳:");
i=(int)Math.floor(Math.random()3+1);
System.out.println(arr[i-1]);
}
}
**我是分页
/**
- 游戏设置
*/
import java.util.Scanner;
public class Game {
Scanner input=new Scanner(System.in);
String[] arr=new String[]{"剪刀","石头","布"};
String pName;//人类名称
String cName;//系统名称
int sum=0;
int peopleWin=0;//人类赢得次数
int computerWin=0;//系统赢得次数
int piece=0;//和平次数
/**
* 主菜单
/
public void initial() {
// TODO Auto-generated method stub
System.out.println("\t");
System.out.println("\t猜拳,开始********");
System.out.println("\t***********");
//System.out.println("请选择对方角色:");
}
/**
* 游戏初始化
* @param people //人类对象
* @param computer //系统对象
*/
public void startGame(People people, Computer computer) {
// TODO Auto-generated method stub
boolean flag=false;
System.out.println("出拳规则: 1.剪刀 2.石头 3.布");
System.out.println("请选择对方角色(1:刘备 2:孙权 3:曹操):");
int nameC=input.nextInt();
if(nameC==1||nameC==2||nameC==3){
cName =computer.id(nameC);
flag=true;
}
else{
System.out.println("角色选择失败!请重新选择!");
startGame(people, computer);
}
if(flag){
System.out.println("请输入你的姓名:");
String nameP=input.next();
pName=people.id(nameP);
System.out.println(pName+" VS "+cName);
}
}
/**
- 比赛结果
- @param p 人类出拳数字
- @param i 系统出拳数字
- @param game //游戏类对象
/
public void showResult(int p, int i, Game game) {
// TODO Auto-generated method stub
sum++;
if((p1&&i3) || (p2&&i1) || (p3&&i2)){
System.out.println("结果:"+"恭喜!"+pName+",你赢了!");
peopleWin++;
}else if(p==i){
System.out.println("您与"+cName+"搭乘了平局");
piece++;
}else{
System.out.println("结果:"+"很抱歉!"+pName+",你输了!");
computerWin++;
}
}
/*- 结果显示
*/
public void result() {
// TODO Auto-generated method stub
System.out.println("----------------------------");
System.out.println(pName+" VS "+cName);
System.out.println("对战总次数:"+sum);
System.out.println("对战和局数:"+piece);
System.out.print("结果:");
if(peopleWin>computerWin){
System.out.println("恭喜人类胜利!系统失败!");
System.out.println("人类胜利次数:"+peopleWin);
}else if(peopleWin==computerWin){
System.out.println("两方打成平手!下次继续一决高下!");
}else{
System.out.println("恭喜系统胜利!人类失败!");
System.out.println("人类胜利次数:"+computerWin);
}
System.out.println("----------------------------");
}
- 结果显示
}