Advertisement
Guest User

Untitled

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