Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2020/4/29(水)~5/2(土)
- import java.util.*;
- public class JankenNoOOP {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Random random = new Random();
- int playCount = 1;
- String playerHandStr = null;
- int inputErrorCount = 0;
- int playerHand = -1;
- int cpuHand = -1;
- int playerWin = 0;
- int draw = 0;
- int cpuWin = 0;
- String nextStr = "y";
- int inputErrorCount2 = 0;
- do {
- System.out.println("じゃんけんをします(" + playCount + "回目)");
- for (int i = 1; i <= 4; i++) {
- System.out.println("出す手を入力して下さい");
- System.out.println("グーならg、チョキならc、パーならpです(半角英字)");
- playerHandStr = scanner.nextLine();
- if (!(playerHandStr.equals("g") || playerHandStr.equals("c") || playerHandStr.equals("p"))) {
- System.out.println("\n半角英字のgかcかpを、1文字入力して下さい");
- inputErrorCount++;
- continue;
- }
- break;
- }
- if (inputErrorCount == 4) {
- System.out.println("正常な入力が行われませんでした");
- break;
- }
- if (playerHandStr.equals("g")) {
- playerHand = 0;
- } else if (playerHandStr.equals("c")) {
- playerHand = 1;
- } else {
- playerHand = 2;
- }
- cpuHand = random.nextInt(3);
- System.out.println("じゃんけん、ポン!\n");
- try {
- Thread.sleep(1000L);
- } catch(InterruptedException e) {
- e.printStackTrace();
- return;
- }
- if (playerHand == 0) {
- System.out.println("あなたの手はグーです");
- } else if (playerHand == 1) {
- System.out.println("あなたの手はチョキです");
- } else {
- System.out.println("あなたの手はパーです");
- }
- if (cpuHand == 0) {
- System.out.println("CPUの手はグーです\n");
- } else if (cpuHand == 1) {
- System.out.println("CPUの手はチョキです\n");
- } else {
- System.out.println("CPUの手はパーです\n");
- }
- try {
- Thread.sleep(2000L);
- } catch(InterruptedException e) {
- e.printStackTrace();
- return;
- }
- if (playerHand == 0 && cpuHand == 1 || playerHand == 1 && cpuHand == 2 || playerHand == 2 && cpuHand == 0) {
- System.out.println("おめでとう!あなたの勝ちです!\n");
- playerWin++;
- } else if (playerHand == cpuHand) {
- System.out.println("おっと、あいこです!\n");
- draw++;
- } else {
- System.out.println("残念!あなたの負けです!\n");
- cpuWin++;
- }
- System.out.println("あなたの勝利数は" + playerWin + "回、あいこの数は" + draw + "回、CPUの勝利数は" + cpuWin + "回です\n");
- for (int i = 1; i <= 4; i++) {
- System.out.println("続けますか?続けるならyを、止めるならnを入力して下さい(半角英字)");
- nextStr = scanner.nextLine();
- if (!(nextStr.equals("y") || nextStr.equals("n"))) {
- System.out.println("\n半角英字のyかnを、1回入力して下さい");
- inputErrorCount2++;
- continue;
- }
- break;
- }
- if (inputErrorCount2 == 4) {
- System.out.println("正常な入力が行われませんでした");
- break;
- }
- if (nextStr.equals("n")) {
- break;
- } else {
- System.out.println();
- }
- inputErrorCount = 0;
- inputErrorCount2 = 0;
- playCount++;
- } while(nextStr.equals("y"));
- System.out.println("終了します");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement