麻烦谁帮我的代码添加注释,因为我要讲解吖,哎哎,不懂的怎么讲,就写在各行代码后面,谢了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;
}
}
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;
}
}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******************"); Scanner input = new Scanner(System.in); System.out.println ("请输入名字:");
String uname =input.next();
person.name=uname; System.out.println("\n\n出拳规则:1.剪刀 2.石头 3.布");
/*选择对方角色*/
System.out.print("请选择角色(1:刘备 2:孙权 3:曹操): ");
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("姓名\t得分");
System.out.println(person.name+"\t"+person.score );
System.out.println(computer.name+"\t"+computer.score );
System.out.println("---------------------------------------------------");
}
/**
* 计算比赛结果
*/
public int calcResult(){
if(person.score == computer.score){
return 1;
}else if(person.score > computer.score){
return 2;
}else{
return 3;
}
}
}
public class StartGuess { /**
* 人机互动版猜拳游戏
* @param args
*/
public static void main(String[] args) {
Game game = new Game();
game.initial();
game.startGame();
}
}
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;
}
}
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;
}
}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******************"); Scanner input = new Scanner(System.in); System.out.println ("请输入名字:");
String uname =input.next();
person.name=uname; System.out.println("\n\n出拳规则:1.剪刀 2.石头 3.布");
/*选择对方角色*/
System.out.print("请选择角色(1:刘备 2:孙权 3:曹操): ");
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("姓名\t得分");
System.out.println(person.name+"\t"+person.score );
System.out.println(computer.name+"\t"+computer.score );
System.out.println("---------------------------------------------------");
}
/**
* 计算比赛结果
*/
public int calcResult(){
if(person.score == computer.score){
return 1;
}else if(person.score > computer.score){
return 2;
}else{
return 3;
}
}
}
public class StartGuess { /**
* 人机互动版猜拳游戏
* @param args
*/
public static void main(String[] args) {
Game game = new Game();
game.initial();
game.startGame();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货