游戏比武java代码 java游戏程序代码( 三 )


通过if判断双方出拳是否相等if(a==0r==0)else if(a==0r==1)else if(a==0r==2)即可实现猜拳,if内直接输出相关语句即可
②记录胜负:定义猜拳方法为int,通过返回值记录相关比赛的胜负情况,可以用0--失败;1--获胜;2--平局 进行记录 , 在主函数中对相应抛出的数字记录即可
if(a==0r==0){
System.out.println("The computer comes out with cloth,it was a draw. ");
return 2;
}
h=comp.compare(a,r);if (h==1)j++;
登录后复制
③玩家决定局数: 定义一个数,在循环中不大于该数即可
④输出获胜、失败及平局: j、k即胜利和失败 , 平局数即n-j-k 。
⑤统计结果,直接用if比较i、j的数字结果即可 。
主函数部分:
package SS2_5;
import java.util.*;
public class Main {
public static void main(String args[]){
Scanner scanner=new Scanner(System.in);
Compare comp=new Compare();
int h=0,j=0,k=0;
System.out.println("rules:0--cloth;1--stone;2--scissors.\nU can choose how many times you want to play:");
int n=scanner.nextInt();
for(int i=1;i=n;i++){
System.out.print("It's the "+i+" round,your turn:");
int a=scanner.nextInt();
int r=new Random().nextInt(3);
switch (a){
case 0:
h=comp.compare(a,r);
break;
case 1:
h=comp.compare(a,r);
break;
case 2:
h=comp.compare(a,r);
break;
default:
System.out.println("Wrong number!");
break;
}
if (h==1)
j++;
else if(h==0)
k++;
}
System.out.println("The total times you won are "+j+",The draw times are "+(n-j-k)+".");
if(jk)
System.out.println("You are the final winner");
else if(kj)
System.out.println("The computer is the winner.");
if(j==k)
System.out.println("The final result is draw");
}
}
登录后复制
compare方法部分
package SS2_5;
public class Compare {
public int compare(int a,int r){
int counter=0;
if(a==0r==0){
System.out.println("The computer comes out with cloth,it was a draw. ");
return 2;
}
else if(a==0r==1){
System.out.println("The computer comes out with stone, you won. ");
return 1;
}
else if(a==0r==2){
System.out.println("The computer comes out with scissor,you lost. ");
return 0;
}
else if(a==1r==0){
System.out.println("The computer comes out with cloth,you lost. ");
return 0;
}
else if(a==1r==1){
System.out.println("The computer comes out with stone,it was a draw. ");
return 2;
}
else if(a==1r==2){
System.out.println("The computer comes out with scissors,you won. ");
return 1;
}
else if(a==2r==0){
System.out.println("The computer comes out with cloth,you won. ");
return 1;
}
else if(a==2r==1){
System.out.println("The computer comes out with stone,you lost. ");
return 0;
}
else if(a==2r==2){
System.out.println("The computer comes out with scissors,it was a draw. ");
return 2;
}
else
return 0;
}
}
登录后复制
java
704拖拉机
精选推荐
广告
java写简单的猜拳游戏
2下载·0评论
2016年7月27日
用Java编写的猜拳小游戏
2029阅读·0评论·0点赞
2021年3月7日
Java猜拳游戏和Random的应用
21阅读·0评论·0点赞
2022年10月24日
java实现完整版猜拳小游戏
25下载·0评论
2018年11月22日
用python实现功能猜拳
1137阅读·2评论·3点赞
2022年7月14日
java猜拳switch计分制_java----猜拳(10局分胜负)
117阅读·0评论·1点赞
2021年3月15日

推荐阅读