main方法类
package caiquan;
import java.util.Scanner;
public class MyMain {
public static void main(String[] args) {
String choose;
Welcome wel = new Welcome();
wel.sayHello();
System.out.println("欢迎进入猜拳游戏");
System.out.println("出拳规则:");
System.out.println("0.石头,1.剪刀,2.布");
do {
System.out.println("要开始吗?y/n");
Game game = new Game();
game.game();
Scanner input = new Scanner(System.in);
System.out.println("是否继续下一局y/n");
choose = input.next();
if (choose.equalsIgnoreCase("n"))
break;
} while (choose.equalsIgnoreCase("y"));
System.out.println("游戏结束");
// System.out.println("你的分数:"+com.getScore());
}
}
计算机类
package caiquan;
public class Computer {
private String comName;
private int score;
private int choose;
public String getComName() {
return comName;
}
public void setComName(String comName) {
this.comName = comName;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getChoose() {
return choose;
}
public void setChoose(int choose) {
this.choose = choose;
}
public void Computer() {
choose = (int) (Math.random() * 3);
// System.out.println(random);
switch (choose) {
case 0:
System.out.println("电脑出拳:石头");
break;
case 1:
System.out.println("电脑出拳:剪刀");
break;
case 2:
System.out.println("电脑出拳:布");
break;
default:
break;
}
}
}
玩家类
package caiquan;
public class User {
private String userName;
private int score;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public User(int choose2) {
switch (choose2) {
case 0:
System.out.println("你出拳:石头");
break;
case 1:
System.out.println("你出拳:剪刀");
break;
case 2:
System.out.println("你出拳:布");
break;
default:
break;
}
}
}
游戏类
package caiquan;
import java.util.Scanner;
public class Game {
public void game() {
// Welcome wel=new Welcome();
// wel.sayHello();
/*
* System.out.println("欢迎进入猜拳游戏"); System.out.println("出拳规则:");
* System.out.println("0.石头,1.剪刀,2.布"); System.out.println("要开始吗?y/n");
*/
Scanner input = new Scanner(System.in);
String ch = input.next();
if (ch.equalsIgnoreCase("y")) {
System.out.println("请出拳:");
System.out.println("0.石头,1.剪刀,2.布(输入数字)");
// 人
int choose2 = input.nextInt();
User user = new User(choose2);
// System.out.println(choose2);
// 电脑
Computer com = new Computer();
com.Computer();
int choose = com.getChoose();
// System.out.println(choose);
System.out.println("结果:");
if ((choose2 == 0 && choose == 1) || (choose2 == 1 && choose == 2) || (choose2 == 2 && choose == 0)) {
System.out.println("你赢了");
int score2 = 0;
score2++;
user.setScore(score2);
} else if ((choose2 == 0 && choose == 0) || (choose2 == 1 && choose == 1)
|| (choose2 == 2 && choose == 2)) {
System.out.println("平局");
} else if ((choose2 == 0 && choose == 2) || (choose2 == 1 && choose == 0)
|| (choose2 == 2 && choose == 1)) {
System.out.println("你输了");
int score = 0;
score++;
com.setScore(score);
}
}
}
}
欢迎类
package caiquan;
import java.util.Scanner;
public class Game {
public void game() {
// Welcome wel=new Welcome();
// wel.sayHello();
/*
* System.out.println("欢迎进入猜拳游戏"); System.out.println("出拳规则:");
* System.out.println("0.石头,1.剪刀,2.布"); System.out.println("要开始吗?y/n");
*/
Scanner input = new Scanner(System.in);
String ch = input.next();
if (ch.equalsIgnoreCase("y")) {
System.out.println("请出拳:");
System.out.println("0.石头,1.剪刀,2.布(输入数字)");
// 人
int choose2 = input.nextInt();
User user = new User(choose2);
// System.out.println(choose2);
// 电脑
Computer com = new Computer();
com.Computer();
int choose = com.getChoose();
// System.out.println(choose);
System.out.println("结果:");
if ((choose2 == 0 && choose == 1) || (choose2 == 1 && choose == 2) || (choose2 == 2 && choose == 0)) {
System.out.println("你赢了");
int score2 = 0;
score2++;
user.setScore(score2);
} else if ((choose2 == 0 && choose == 0) || (choose2 == 1 && choose == 1)
|| (choose2 == 2 && choose == 2)) {
System.out.println("平局");
} else if ((choose2 == 0 && choose == 2) || (choose2 == 1 && choose == 0)
|| (choose2 == 2 && choose == 1)) {
System.out.println("你输了");
int score = 0;
score++;
com.setScore(score);
}
}
}
}