只用一个类编写的java

核心提示import java.util.Map;import java.util.HashMap;import java.util.Random;import java.io.BufferedReader;import java.io.Input

import java.util.Map;

import java.util.HashMap;

import java.util.Random;

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class Stone{

public static void main(String []agrs) throws Exception{

Map st=new HashMap();

st.put(1,"石头");

st.put(2,"剪刀");

st.put(3,"布");

Random co=new Random();

while(true){

System.out.println("请输入你需要你的选择 1:石头 2:剪刀 3:布 4:退出");

int x=Math.abs(co.nextInt()%3)+1;

//System.out.println(x); //用于测试电脑的随即选择

BufferedReader me=new BufferedReader(new InputStreamReader(System.in));

String an=me.readLine();

try{

if(an.length()>1||Integer.parseInt(an)<1||Integer.parseInt(an)>4){

System.out.println("请输入正确的选择");

continue;

}

}catch(Exception e){

System.out.println("请输入正确的选择");

continue;

}

int z=Integer.parseInt(an);

if(z==4){

return;

}

else{

if(x==z){

System.out.println("电脑为:"+st.get(x));

System.out.println("你的为:"+st.get(z));

System.out.println("打和");

}

else if(x==1&&z==2){

System.out.println("电脑为:"+st.get(x));

System.out.println("你的为:"+st.get(z));

System.out.println("你赢了");

}

else if(x==1&&z==3){

System.out.println("电脑为:"+st.get(x));

System.out.println("你的为:"+st.get(z));

System.out.println("你输了");

}

else if(x==2&&z==1){

System.out.println("电脑为:"+st.get(x));

System.out.println("你的为:"+st.get(z));

System.out.println("你赢了");

}

else if(x==2&&z==3){

System.out.println("电脑为:"+st.get(x));

System.out.println("你的为:"+st.get(z));

System.out.println("你输了");

}

else if(x==3&&z==1){

System.out.println("电脑为:"+st.get(x));

System.out.println("你的为:"+st.get(z));

System.out.println("你赢了");

}

else{

System.out.println("电脑为:"+st.get(x));

System.out.println("你的为:"+st.get(z));

System.out.println("你输了");

}

}

System.out.println();

System.out.println();

}

}

}

//猜拳,1石头,2剪刀,3,布

//随机数的输入Random类

import?java.util.*;

public?class?secondDay4?

{

public?static?void?main(String?args[])

{

Game?game=new?Game();

game.money();

game.rule();

game.times();

}

}

class?Game{

Scanner?s=new?Scanner(System.in);

private?int?me;

private?int?it;

private?int?money;

private?static?int?aggregate;

private?static?int?difference;

private?static?int?temp;

private?int?timesWin;

private?int?timesLose;

private?int?timesDraw;

//构造函数,初始化情景

public?Game(){

System.out.println("***欢迎来到“石头剪子布”猜拳俱乐部***");

System.out.println("************游戏规则************");

System.out.println("1、石头n2、剪刀n3、布");

}

//定义游戏币类,初始化游戏币的值

public?void?money(){

System.out.println("请选择以何种身份进入俱乐部");

System.out.println("***1普通会员***");

System.out.println("***2银卡会员***");

System.out.println("***3金卡会员***");

for(int?i=1;i>0;i++){

this.money=s.nextInt();

if(money==1){

System.out.println("您的游戏币为100,开始游戏!");

aggregate=100;

break;

}else?if(money==2){

System.out.println("您的游戏币为1000,开始游戏!");

aggregate=1000;

break;

}else?if(money==3){

System.out.println("您的游戏币为5000,开始游戏!");

aggregate=5000;

break;

}else{

System.out.println("你手抖了o(╯□╰)o```重新选```");

}

}

}

//定义余额方法,计算余额

public?static?int?difference(){

difference=aggregate+temp;

return?difference;

}

//定义times方法,计算游戏次数

public?void?times(){

System.out.println("您的比赛成绩:");

System.out.println("胜利:"+this.timesWin+"次");

System.out.println("平局:"+this.timesDraw+"次");

System.out.println("失败:"+this.timesLose+"次");

}

//定义rule方法,写入游戏规则,并完成temp?的累加等

public?void?rule(){

for(int?i=1;i>0;i++){

this.me=new?Random().nextInt(3)+1;

System.out.println("请出拳:");

this.it=s.nextInt();

if(this.me==this.it)

{

System.out.println("平局!");

timesDraw+=1;

}else?if(this.me==1&&this.it==2)

{

System.out.println("恭喜你,你赢了!");

temp+=50;

timesWin+=1;

}else?if(this.me==1&&this.it==3){

System.out.println("很遗憾,你输了!");

temp+=-50;

timesLose+=1;

}else?if(this.me==2&&this.it==1){

System.out.println("很遗憾,你输了!");

temp+=-50;

timesLose+=1;

}else?if(this.me==2&&this.it==3){

System.out.println("恭喜你,你赢了!");

temp+=50;

timesWin+=1;

}else?if(this.me==3&&this.it==1){

System.out.println("恭喜你,你赢了!");

temp+=50;

timesWin+=1;

}else?if(this.me==3&&this.it==2){

System.out.println("很遗憾,你输了!");

temp+=-50;

timesLose+=1;

}else{

System.out.println("喂!你有没脑子啊?这也会输错?");

}

System.out.println("您的余额是:"+difference());

if(difference()==0){

System.out.println("你没钱啦!O(∩_∩)O哈哈~欢迎下次再来!");

break;

}

System.out.println("还要继续吗?选择1继续游戏,选择2退出游戏");

int?change=s.nextInt();

if(change==1){

System.out.println("第"+i+"轮游戏开始!");

}else?if(change==2){

break;

}else{

System.out.println("操作错误,强行退出");

break;

}

}

}

}

上面是正确的猜拳游戏,如果想要每次人都赢,只要修改public void rule()方法,吧平局和输了里面的内容都改为:

System.out.println("恭喜你,你赢了!");

temp+=50;

timesWin+=1;

 
友情链接
鄂ICP备19019357号-22