java里,石头剪刀布的小程序怎么写的
2个回答
展开全部
我原来写过一个,比你说的复杂一点你可以参考下。代码如下
import java.util.Scanner;
import java.util.Random;
public class Game {
//Player player;
//Computer computer;
int count = 0;
Scanner input = new Scanner(System.in);
Computer computer = new Computer();
Player player = new Player();
Random run = new Random();
public void playerChange() {
System.out.println("------------------------------------- 欢 迎 进 入 游 戏 世 界 -------------------------------------\n\n");
System.out.println(" **********************");
System.out.println(" ** 猜拳,开始 **");
System.out.println(" **********************\n\n\n");
System.out.println("出拳规则:1、剪刀 2、石头 3、布");
System.out.print("请选择人物: 1、曹操 2、刘备 3、孙权\n\n");
int no = input.nextInt();
int r = run.nextInt();
switch(no) {
case 1:
player.name = "曹操";
if(r > no) {
computer.name = "刘备";
}
else {
computer.name = "孙权";
}
break;
case 2:
player.name = "刘备";
if(r > no) {
computer.name = "曹操";
}
else {
computer.name = "孙权";
}
break;
case 3:
player.name = "孙权";
if(r > no) {
computer.name = "刘备";
}
else {
computer.name = "曹操";
}
}
System.out.println(player.name + "\tVS\t" + computer.name);
attack();
}
public void attack() {
System.out.println("要开始么?y/n\n\n");
String no = input.next();
if(no.equals("n")) {
over();
}
do {
int perFist=player.showFist();
int comFist = computer.showFist();
System.out.println("回合数:" + count);
if(perFist==1 && comFist == 2 || perFist == 2 &&comFist ==3 ||perFist == 3 && comFist == 1 ) {
System.out.println("结果:^_^你输了,真笨!");
count++;
computer.score++;
}
else if(comFist == perFist) {
System.out.println("结果:和局,真衰,嘿嘿,等着瞧吧!");
count++;
}
else {
System.out.println("结果:恭喜!你赢了!");
count++;
player.score++;
}
System.out.println(player.name + "积分为:" + player.score + "\t\t" + computer.name + "积分为:" + computer.score);
System.out.println("是否继续?y/n");
no = input.next();
}
while(no.equals("y"));
System.out.println("----------------------------------------------------------------------------------------");
if(player.score > computer.score) {
System.out.println("最终结果:" +player.name + "在" + count + "回合中战胜了" + computer.name);
}
else if(player.score < computer.score) {
System.out.println("最终结果:" +computer.name + "在" + count + "回合中战胜了" + player.name);
}
else {
System.out.println("最终结果:" +player.name + "在" + count + "回合中和" + computer.name + "战平");
}
System.out.println("----------------------------------------------------------------------------------------");
over();
}
public void over(){
System.out.println("GAME OVER!");
}
}
import java.util.Random;
public class Computer {
String name;
int score;
public int showFist() {
Random run = new Random();
int moves = run.nextInt(3) + 1;
switch(moves) {
case 1:
System.out.println("电脑出:剪刀\n");
break;
case 2:
System.out.println("电脑出:石头\n");
break;
case 3:
System.out.println("电脑出:布\n");
}
return moves;
}
}
import java.util.Scanner;
public class Player {
String name;
int score;
public int showFist() {
Scanner input = new Scanner(System.in);
System.out.println("请选择招式:1、剪刀 2、石头 3、布");
int moves = input.nextInt();
switch(moves) {
case 1:
System.out.println("玩家出:剪刀");
break;
case 2:
System.out.println("玩家出:石头");
break;
case 3:
System.out.println("玩家出:布");
}
return moves;
}
}
public class Main {
public static void main(String[] args) {
Game game = new Game();
game.playerChange();
}
}
import java.util.Scanner;
import java.util.Random;
public class Game {
//Player player;
//Computer computer;
int count = 0;
Scanner input = new Scanner(System.in);
Computer computer = new Computer();
Player player = new Player();
Random run = new Random();
public void playerChange() {
System.out.println("------------------------------------- 欢 迎 进 入 游 戏 世 界 -------------------------------------\n\n");
System.out.println(" **********************");
System.out.println(" ** 猜拳,开始 **");
System.out.println(" **********************\n\n\n");
System.out.println("出拳规则:1、剪刀 2、石头 3、布");
System.out.print("请选择人物: 1、曹操 2、刘备 3、孙权\n\n");
int no = input.nextInt();
int r = run.nextInt();
switch(no) {
case 1:
player.name = "曹操";
if(r > no) {
computer.name = "刘备";
}
else {
computer.name = "孙权";
}
break;
case 2:
player.name = "刘备";
if(r > no) {
computer.name = "曹操";
}
else {
computer.name = "孙权";
}
break;
case 3:
player.name = "孙权";
if(r > no) {
computer.name = "刘备";
}
else {
computer.name = "曹操";
}
}
System.out.println(player.name + "\tVS\t" + computer.name);
attack();
}
public void attack() {
System.out.println("要开始么?y/n\n\n");
String no = input.next();
if(no.equals("n")) {
over();
}
do {
int perFist=player.showFist();
int comFist = computer.showFist();
System.out.println("回合数:" + count);
if(perFist==1 && comFist == 2 || perFist == 2 &&comFist ==3 ||perFist == 3 && comFist == 1 ) {
System.out.println("结果:^_^你输了,真笨!");
count++;
computer.score++;
}
else if(comFist == perFist) {
System.out.println("结果:和局,真衰,嘿嘿,等着瞧吧!");
count++;
}
else {
System.out.println("结果:恭喜!你赢了!");
count++;
player.score++;
}
System.out.println(player.name + "积分为:" + player.score + "\t\t" + computer.name + "积分为:" + computer.score);
System.out.println("是否继续?y/n");
no = input.next();
}
while(no.equals("y"));
System.out.println("----------------------------------------------------------------------------------------");
if(player.score > computer.score) {
System.out.println("最终结果:" +player.name + "在" + count + "回合中战胜了" + computer.name);
}
else if(player.score < computer.score) {
System.out.println("最终结果:" +computer.name + "在" + count + "回合中战胜了" + player.name);
}
else {
System.out.println("最终结果:" +player.name + "在" + count + "回合中和" + computer.name + "战平");
}
System.out.println("----------------------------------------------------------------------------------------");
over();
}
public void over(){
System.out.println("GAME OVER!");
}
}
import java.util.Random;
public class Computer {
String name;
int score;
public int showFist() {
Random run = new Random();
int moves = run.nextInt(3) + 1;
switch(moves) {
case 1:
System.out.println("电脑出:剪刀\n");
break;
case 2:
System.out.println("电脑出:石头\n");
break;
case 3:
System.out.println("电脑出:布\n");
}
return moves;
}
}
import java.util.Scanner;
public class Player {
String name;
int score;
public int showFist() {
Scanner input = new Scanner(System.in);
System.out.println("请选择招式:1、剪刀 2、石头 3、布");
int moves = input.nextInt();
switch(moves) {
case 1:
System.out.println("玩家出:剪刀");
break;
case 2:
System.out.println("玩家出:石头");
break;
case 3:
System.out.println("玩家出:布");
}
return moves;
}
}
public class Main {
public static void main(String[] args) {
Game game = new Game();
game.playerChange();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询