publicstaticvoidmain(String[]args){
Scannerin=newScanner(System.in);
System.out.println("-----猜拳游戏-----");
Stringb;
do{
System.out.println("请输入:1.剪刀2.石头3.布");
intren=in.nextInt();
while(ren>3||ren<1){
System.out.print("请正确输入数字:");
ren=in.nextInt();
}
intjisuanji=(int)(Math.random()*3)+1;
Stringren1="标记";
Stringjisuanji1="标记";
switch(ren){
case1:
ren1="剪刀";
break;
case2:
ren1="石头";
break;
case3:
ren1="布";
break;
}
switch(jisuanji){
case1:
jisuanji1="剪刀";
break;
case2:
jisuanji1="石头";
break;
case3:
jisuanji1="布";
break;
}
if(ren==jisuanji){
System.out.println("你出的是"+ren1+"电脑出的是:"+jisuanji1
+"-_-平局");
}elseif(ren==1&&jisuanji==2||ren==2&&jisuanji==3
||ren==3&&jisuanji==1){
System.out.println("你出的是"+ren1+"电脑出的是:"+jisuanji1
+"--你输了");
}else{
System.out.println("你出的是:"+ren1+"电脑出的是:"+jisuanji1
+"^_^你赢了!");
}
System.out.println("是否继续Y/N");
b=in.next();
while(!b.equalsIgnoreCase("Y")&&!b.equalsIgnoreCase("N")){
System.out.print("请输入正确字母:");
b=in.next();
}
}while(b.equalsIgnoreCase("Y"));
System.out.println("多谢你的支持,再见!");
}
/*
另外,如果输入数字的地方输入了字母就算判断了也不能继续输入的哈,因为这样会使得程序异常,只能结束程序运行
*/
importjava.util.Scanner;
publicclassGame{
privateScannerscan=newScanner(System.in);
privateStringrule[]={"","剪刀","石头","布"};
privateStringrole[]={"","刘备","孙权","曹操"};
privateComputercomputer;
privateUseruser;
privateintroundCount;
privateGame(){
roundCount=0;
}
publicstaticvoidmain(String[]args){
Gamegame=newGame();
game.start();
}
publicvoidstart(){
computer=newComputer();
System.out.println("--欢迎进入游戏世界--");
System.out.println("********************");
System.out.println("**猜拳,开始**");
System.out.println("********************");
System.out.println();
System.out.println("出拳规则:1.剪刀2.石头3.布");
System.out.print("请选择角色(1:刘备2.孙权3.曹操)");
intuserRole=scan.nextInt();
if(userRole>=1&&userRole<=3){
user=newUser(role[userRole]);
System.out.println();
System.out.print("要开始吗?(y/n)");
Stringstart=scan.next();
if(start.equals("y")){
round();
}
elseif(start.equals("n")){
}
}
}
privatevoidround(){
roundCount++;
System.out.println();
System.out.print("请出拳:1.剪刀2.石头3.布(输入相应数字):");
intuserRule=user.round();
if(userRule>=1&&userRule<=3){
intcomputerRule=computer.round();
judge(userRule,computerRule);
System.out.println();
System.out.print("是否开始下一轮(y/n):");
Stringnext=scan.next();
if(next.equals("y")){
round();
}
elseif(next.equals("n")){
end();
}
}
}
privatevoidjudge(intur,intcr){
System.out.println("你出拳:"+rule[ur]);
System.out.println("电脑出拳:"+rule[cr]);
if(ur==cr){
System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!");
}
elseif((ur==1&&cr==3)||
(ur==2&&cr==1)||
(ur==3&&cr==2)){
System.out.println("结果:你赢了!");
user.win();
}
else{
System.out.println("结果:你输了!");
computer.win();
}
}
privatevoidend(){
System.out.println("--------------------------------------");
System.out.println(user.getName()+"VS"+computer.getName());
System.out.println("对战次数:"+roundCount);
if(computer.getWin()==user.getWin()){
System.out.println("结果:打成平手,下次再和你一分高下!");
}
elseif(computer.getWin()>user.getWin()){
System.out.println("结果:你输了!电脑赢了"+computer.getWin()+"次!");
}
else{
System.out.println("结果:你赢了!你赢了"+user.getWin()+"次!");
}
System.out.println("--------------------------------------");
}
classComputer{
privateintwin;
privateStringname;
publicComputer(){
win=0;
name="匿名";
}
publicintround(){
return(int)(System.currentTimeMillis()%3)+1;//随机返回1、2、3
}
publicStringgetName(){
returnname;
}
publicvoidwin(){
win++;
}
publicintgetWin(){
returnwin;
}
}
classUser{
privateintwin;
privateStringname="";
publicUser(Stringname){
this.name=name;
win=0;
}
publicintround(){
returnscan.nextInt();
}
publicStringgetName(){
returnname;
}
publicvoidwin(){
win++;
}
publicintgetWin(){
returnwin;
}
}
}
刚刚写好的,应该满足你的题目要求。赢了和输了的提示信息题目里没有是我自己随便写的。
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();
}
}
}
上一篇:it小知识8款免费编程游戏
下一篇:Java打地鼠游戏需要的知识点