Advertisement
Guest User

Untitled

a guest
Apr 30th, 2020
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. // 2020/4/29(水)~5/1(金)
  2.  
  3. import java.util.*;
  4.  
  5. public class JankenNoOOP {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. Random random = new Random();
  9. int playCount = 1;
  10. String playerHandStr = null;
  11. int inputErrorCount = 0;
  12. int playerHand = -1;
  13. int cpuHand = -1;
  14. int playerWin = 0;
  15. int cpuWin = 0;
  16. String isNextStr = null;
  17. int inputErrorCount2 = 0;
  18. boolean isNext = true;
  19.  
  20. do {
  21. System.out.println("じゃんけんをします(" + playCount + "回目)");
  22. for (int i = 1; i <= 4; i++) {
  23. System.out.println("出す手を入力して下さい");
  24. System.out.println("グーなら0、チョキなら1、パーなら2です(半角数字)");
  25. playerHandStr = scanner.nextLine();
  26. if (!(playerHandStr.equals("0") || playerHandStr.equals("1") || playerHandStr.equals("2"))) {
  27. System.out.println("\n半角数字の0か1か2を、1文字入力して下さい");
  28. inputErrorCount++;
  29. continue;
  30. }
  31. playerHand = Integer.parseInt(playerHandStr);
  32. break;
  33. }
  34. if (inputErrorCount == 4) {
  35. System.out.println("正常な入力が行われませんでした");
  36. break;
  37. }
  38. cpuHand = random.nextInt(3);
  39. System.out.println("じゃんけん、ポン!\n");
  40. try {
  41. Thread.sleep(1000L);
  42. } catch(InterruptedException e) {
  43. e.printStackTrace();
  44. return;
  45. }
  46. if (playerHand == 0) {
  47. System.out.println("あなたの手はグーです");
  48. } else if (playerHand == 1) {
  49. System.out.println("あなたの手はチョキです");
  50. } else {
  51. System.out.println("あなたの手はパーです");
  52. }
  53. if (cpuHand == 0) {
  54. System.out.println("CPUの手はグーです\n");
  55. } else if (cpuHand == 1) {
  56. System.out.println("CPUの手はチョキです\n");
  57. } else {
  58. System.out.println("CPUの手はパーです\n");
  59. }
  60. try {
  61. Thread.sleep(2000L);
  62. } catch(InterruptedException e) {
  63. e.printStackTrace();
  64. return;
  65. }
  66. if (playerHand == 0 && cpuHand == 1 || playerHand == 1 && cpuHand == 2 || playerHand == 2 && cpuHand == 0) {
  67. System.out.println("おめでとう!あなたの勝ちです!");
  68. playerWin++;
  69. } else if (playerHand == cpuHand) {
  70. System.out.println("おっと、あいこです!");
  71. } else {
  72. System.out.println("残念!あなたの負けです!");
  73. cpuWin++;
  74. }
  75. System.out.println("あなたの勝利数は" + playerWin + "回、CPUの勝利数は" + cpuWin + "回です\n");
  76. for (int i = 1; i <= 4; i++) {
  77. System.out.println("続けますか?続けるならtrueを、止めるならfalseを入力して下さい(半角英字)");
  78. isNextStr = scanner.nextLine();
  79. if (!(isNextStr.equals("true") || isNextStr.equals("false"))) {
  80. System.out.println("\n半角英字のtrueかfalseを、1回入力して下さい");
  81. inputErrorCount2++;
  82. continue;
  83. }
  84. isNext = Boolean.parseBoolean(isNextStr);
  85. break;
  86. }
  87. if (inputErrorCount2 == 4) {
  88. System.out.println("正常な入力が行われませんでした");
  89. break;
  90. }
  91. if (!isNext) {
  92. break;
  93. } else {
  94. System.out.println();
  95. }
  96. inputErrorCount = 0;
  97. inputErrorCount2 = 0;
  98. playCount++;
  99. } while(isNext);
  100. System.out.println("終了します");
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement