发布网友 发布时间:2022-04-22 11:31
共5个回答
热心网友 时间:2023-10-12 14:32
import java.util.Scanner;
public class Game {
private Scanner scan = new Scanner(System.in);
private String rule[] = {"","剪刀","石头","布"};
private String role[] = {"","刘备","孙权","曹操"};
private Computer computer;
private User user;
private int roundCount;
private Game(){
roundCount = 0;
}
public static void main(String[] args) {
Game game = new Game();
game.start();
}
public void start(){
computer = new Computer();
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.曹操)");
int userRole = scan.nextInt();
if(userRole >=1 && userRole <=3){
user = new User(role[userRole]);
System.out.println();
System.out.print("要开始吗?(y/n) ");
String start = scan.next();
if(start.equals("y")){
round();
}
else if(start.equals("n")){
}
}
}
private void round(){
roundCount ++;
System.out.println();
System.out.print("请出拳:1.剪刀 2.石头 3.布 (输入相应数字): ");
int userRule = user.round();
if(userRule >= 1 && userRule <= 3){
int computerRule = computer.round();
judge(userRule,computerRule);
System.out.println();
System.out.print("是否开始下一轮(y/n): ");
String next = scan.next();
if(next.equals("y")){
round();
}
else if(next.equals("n")){
end();
}
}
}
private void judge(int ur,int cr){
System.out.println("你出拳:"+ rule[ur]);
System.out.println("电脑出拳:"+ rule[cr]);
if(ur == cr){
System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!");
}
else if((ur == 1 && cr== 3)||
(ur == 2 && cr == 1)||
(ur == 3 && cr == 2)){
System.out.println("结果:你赢了!");
user.win();
}
else{
System.out.println("结果:你输了!");
computer.win();
}
}
private void end(){
System.out.println("- -------------------------------------");
System.out.println(user.getName() + "VS" + computer.getName());
System.out.println("对战次数:" + roundCount);
if(computer.getWin() == user.getWin()){
System.out.println("结果:打成平手,下次再和你一分高下!");
}
else if(computer.getWin() > user.getWin()){
System.out.println("结果:你输了!电脑赢了" + computer.getWin()+"次!");
}
else{
System.out.println("结果:你赢了!你赢了" + user.getWin()+"次!");
}
System.out.println("- -------------------------------------");
}
class Computer{
private int win;
private String name;
public Computer(){
win = 0;
name = "匿名";
}
public int round(){
return (int)(System.currentTimeMillis() % 3) + 1;//随机返回1、2、3
}
public String getName(){
return name;
}
public void win(){
win ++;
}
public int getWin(){
return win;
}
}
class User{
private int win;
private String name = "";
public User(String name){
this.name = name;
win = 0;
}
public int round(){
return scan.nextInt();
}
public String getName(){
return name;
}
public void win(){
win ++;
}
public int getWin(){
return win;
}
}
}
刚刚写好的,应该满足你的题目要求。赢了和输了的提示信息题目里没有是我自己随便写的。
热心网友 时间:2023-10-12 14:32
我之前写了个猜拳游戏的源代码,不过没你想的这么精彩。你才给5分就给你你自己修改了,应该很简单的。要多给点分我可以帮你修改。
import java.util.Scanner;
import java.util.Random;
public class caiquan
{
final int jian=0;
final int shitou=1;
final int bu=2;
public static void main(String[] args)
{
String yn="y";
while (yn.equals("y"))
{
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎玩猜拳游戏。请输入0,1,2:0表示剪刀,1表示石头,2表示布");
int a = scanner.nextInt();
Random rd = new Random();
int b = rd.nextInt(3);
switch (b)
{
case 0:
{
System.out.println("系统出的是剪刀");
switch(a)
{
case 0:System.out.println("平");break;
case 1:System.out.println("赢");break;
case 2:System.out.println("输");break;
}
}
break;
case 1:
{
System.out.println("系统出的是石头");
switch(a)
{
case 0:System.out.println("输");break;
case 1:System.out.println("平");break;
case 2:System.out.println("赢");break;
}
}
break;
case 2:
{
System.out.println("系统出的是布");
switch(a)
{
case 0:System.out.println("赢");break;
case 1:System.out.println("输");break;
case 2:System.out.println("平");break;
}
}
}
Scanner ynn = new Scanner(System.in);
System.out.println("是否继续?是请输入y,否则输入n。");
yn=ynn.next();
}
}
}
热心网友 时间:2023-10-12 14:33
import java.util.Scanner;
abstract class Computer{
abstract void playGame();
}
public class Game extends Computer {
public static void main(String[] args) {
new Game().playGame();
}
static Scanner sc = new Scanner(System.in);
static String[] games = {"猜拳游戏"};
static String[] roles = {"刘备", "孙权", "曹操"};
static final String[] MORA = {"石头", "剪刀", "布"};
static int playCount = 0;
static String currentRole = "";
@Override
void playGame() {
doMoragame();
}
public void doMoragame(){
System.out.println("- - 欢迎进入游戏世界 - -");
System.out.println("** ******************");
System.out.println("** 猜拳,开始 **");
System.out.println("** ******************");
System.out.println("出拳规则:1.剪刀 2.石头 3.布");
String startMsg = "请选择角色:";
for (int i = 0; i < roles.length; i++) {
startMsg += (i + 1) + "." + roles[i];
}
System.out.println(startMsg);
String input = sc.nextLine();
currentRole = roles[selectRole(input)];
System.out.println("要开始吗?(y/n");
String isStart = sc.nextLine();
if ("y".equals(isStart)) {
gameStart();
} else {
System.out.println("结束");
sc.close();
return;
}
}
int selectRole(String input){
int index = 0;
switch (input) {
case "1":
index = 0;
break;
case "2":
index = 1;
break;
case "3":
index = 2;
break;
default:
break;
}
return index;
}
void gameStart(){
playCount ++;
System.out.println("请出拳:1.石头 2.剪刀 3.布");
String input = sc.nextLine();
try{
int index = Integer.valueOf(input);
System.out.println("你出拳:" + MORA[index - 1]);
int computerIndex = (int)Math.ceil(Math.random() * 3);
System.out.println("电脑出拳:" + MORA[computerIndex - 1]);
System.out.println(currentRole + "VS电脑");
System.out.println("对战次数:" + playCount);
if (index == computerIndex) {
System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!");
} else if (computerIndex == 0 && index == 1) {
System.out.println("结果:你输了");
} else if (computerIndex == 1 && index == 2) {
System.out.println("结果:你输了");
} else if (computerIndex == 2 && index == 0) {
System.out.println("结果:你输了");
} else {
System.out.println("结果:你赢了");
}
} catch (Exception e){
e.printStackTrace();
System.err.println("输入异常参数!!!");
} finally{
System.out.println("是否开始下一轮(y/n)");
String finalInput = sc.nextLine();
if ("y".equals(finalInput)) {
gameStart();
} else {
System.out.println("结束");
sc.close();
return;
}
}
}
}
热心网友 时间:2023-10-12 14:34
我之前写了个猜拳游戏的源代码,不过没你想的这么精彩。你才给5分就给你你自己修改了,应该很简单的。要多给点分我可以帮你修改。
import java.util.Scanner;
import java.util.Random;
public class caiquan
{
final int jian=0;
final int shitou=1;
final int bu=2;
public static void main(String[] args)
{
String yn="y";
while (yn.equals("y"))
{
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎玩猜拳游戏。请输入0,1,2:0表示剪刀,1表示石头,2表示布");
int a = scanner.nextInt();
Random rd = new Random();
int b = rd.nextInt(3);
switch (b)
{
case 0:
{
System.out.println("系统出的是剪刀");
switch(a)
{
case 0:System.out.println("平");break;
case 1:System.out.println("赢");break;
case 2:System.out.println("输");break;
}
}
break;
case 1:
{
System.out.println("系统出的是石头");
switch(a)
{
case 0:System.out.println("输");break;
case 1:System.out.println("平");break;
case 2:System.out.println("赢");break;
}
}
break;
case 2:
{
System.out.println("系统出的是布");
switch(a)
{
case 0:System.out.println("赢");break;
case 1:System.out.println("输");break;
case 2:System.out.println("平");break;
}
}
}
Scanner ynn = new Scanner(System.in);
System.out.println("是否继续看是请输入y,否则输入n。");
yn=ynn.next();
}
}
}
热心网友 时间:2023-10-12 14:34
老哥,这个程序好歹几十块钱,你就给10个金币 - -