Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2020/4/29(水)~5/1(金)
- 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 cpuWin = 0;
- String isNextStr = null;
- int inputErrorCount2 = 0;
- boolean isNext = true;
- do {
- System.out.println("じゃんけんをします(" + playCount + "回目)");
- for (int i = 1; i <= 4; i++) {
- System.out.println("出す手を入力して下さい");
- System.out.println("グーなら0、チョキなら1、パーなら2です(半角数字)");
- playerHandStr = scanner.nextLine();
- if (!(playerHandStr.equals("0") || playerHandStr.equals("1") || playerHandStr.equals("2"))) {
- System.out.println("\n半角数字の0か1か2を、1文字入力して下さい");
- inputErrorCount++;
- continue;
- }
- playerHand = Integer.parseInt(playerHandStr);
- break;
- }
- if (inputErrorCount == 4) {
- System.out.println("正常な入力が行われませんでした");
- break;
- }
- 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("おめでとう!あなたの勝ちです!");
- playerWin++;
- } else if (playerHand == cpuHand) {
- System.out.println("おっと、あいこです!");
- } else {
- System.out.println("残念!あなたの負けです!");
- cpuWin++;
- }
- System.out.println("あなたの勝利数は" + playerWin + "回、CPUの勝利数は" + cpuWin + "回です\n");
- for (int i = 1; i <= 4; i++) {
- System.out.println("続けますか?続けるならtrueを、止めるならfalseを入力して下さい(半角英字)");
- isNextStr = scanner.nextLine();
- if (!(isNextStr.equals("true") || isNextStr.equals("false"))) {
- System.out.println("\n半角英字のtrueかfalseを、1回入力して下さい");
- inputErrorCount2++;
- continue;
- }
- isNext = Boolean.parseBoolean(isNextStr);
- break;
- }
- if (inputErrorCount2 == 4) {
- System.out.println("正常な入力が行われませんでした");
- break;
- }
- if (!isNext) {
- break;
- } else {
- System.out.println();
- }
- inputErrorCount = 0;
- inputErrorCount2 = 0;
- playCount++;
- } while(isNext);
- System.out.println("終了します");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement