java课后作业一~石头剪刀布的问题

Python022

java课后作业一~石头剪刀布的问题,第1张

import java.util.Scanner

public class EasyGame {

/**

* 关于类及属性的权限问题请自行考虑设置,目前仅供参考

*/

int i// 循环索引

int n// n轮

int na// 小a出拳周期长度

int nb// 小b出拳周期长度

int[] naArr// 小a出拳规律

int[] nbArr// 小b出拳规律

int whoWin// 0为平手,大于0小a赢,小于0小b赢

Scanner sc = new Scanner(System.in)

public void init() {

// System.out.ptintln("如有需要可在此自行添加友好提示")

n = sc.nextInt()// 初始化出拳轮数

na = sc.nextInt()// 初始化小a出拳周期长度

nb = sc.nextInt()// 初始化小b出拳周期长度

initA()// 初始化小a出拳规律

initB()// 初始化小b出拳规律

}

public void initA() {

// System.out.ptintln("如有需要可在此自行添加友好提示")

naArr = new int[na]

for (i = 0 i < na i++) {

naArr[i] = sc.nextInt()

}

}

public void initB() {

// System.out.ptintln("如有需要可在此自行添加友好提示")

nbArr = new int[nb]

for (i = 0 i < nb i++) {

nbArr[i] = sc.nextInt()

}

}

public void showResult() {

// System.out.ptintln("如有需要可在此自行添加友好提示")

whoWin = 0// 默认为平手

for (i = 0 i < n i++) {

//System.out.println(naArr[i % na] + "," + nbArr[i % nb]+"")

switch (naArr[i % na] - nbArr[i % nb]) {

case -5:// 石头,布

whoWin--

break

case -3:// 剪刀,布

whoWin++

break

case -2:// 石头,剪刀

whoWin++

break

case 2:// 剪刀,石头

whoWin--

break

case 3:// 布,剪刀

whoWin--

break

case 5:// 布,石头

whoWin++

break

default:

break

}

}

if (whoWin > 0) {

System.out.println("A")

} else if (whoWin == 0) {

System.out.println("B")

} else {

System.out.println("draw")

}

}

public static void main(String[] args) {

// 若需程序一直运行,可自行添加循环及条件

EasyGame eGame = new EasyGame()

eGame.init()

eGame.showResult()

}

}

试了可以哦,记得采纳

import java.util.Random

import java.util.Scanner

public class FingerGuessingGame {

private static Scanner sc

private static Random rad

private static final String[] FINGERS = {"剪刀", "石头", "布"}

private static int win = 0, loose = 0, draw = 0

public static void main(String[] args) {

sc = new Scanner(System.in)

rad = new Random()

while(true) {

System.out.println("~~~~~~~~~~~~剪刀石头布游戏,输入E可以退出~~~~~~~~~~~")

System.out.println("请选择你要出什么?Z——剪刀,X——石头,C——布")

String command = sc.nextLine()

int playerFinger = getValue(command)

if(playerFinger == -1) {

break

} else if(playerFinger == 3) {

System.out.println("输入错误,请参考说明!")

continue

}

System.out.println("你出的是" + FINGERS[playerFinger])

int cpuFinger = rad.nextInt(3)

System.out.println("计算机出的是" + FINGERS[cpuFinger])

int result = playerFinger - cpuFinger

if(0 == result) {

System.out.println("平局!")

draw ++

} else if(-1 == result) {

System.out.println("你输了!")

loose ++

} else {

System.out.println("你赢了!")

win ++

}

}

System.out.println("游戏结束!\r\n游戏统计次数")

System.out.println(String.format("赢:%d\r\n输:%d\r\n平局:%d", win, loose, draw))

}

private static int getValue(String command) {

if(command.equalsIgnoreCase("E")) {

return -1

}

if(command.equalsIgnoreCase("Z")) {

return 0

}

if(command.equalsIgnoreCase("X")) {

return 1

}

if(command.equalsIgnoreCase("C")) {

return 2

}

return 3

}

}