Advertisement
Guest User

Untitled

a guest
Dec 27th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. // 2019/12/28(土)
  2. // 性善説コード
  3.  
  4. import java.util.*;
  5.  
  6. public class Janken {
  7. public static void main(String[] args) {
  8. boolean isPlay = true;
  9. String youStr;
  10. int you;
  11. int cpu;
  12. Scanner scanner = new Scanner(System.in);
  13. Random random = new Random();
  14.  
  15. System.out.println("ジャンケンをします");
  16. while (isPlay) {
  17. System.out.println("グーなら0、チョキなら1、パーなら2を入力して下さい");
  18. you = scanner.nextInt();
  19. if (you < 0 || you >2) {
  20. System.out.println("0、1、2のいずれかを入力して下さい\n");
  21. continue;
  22. }
  23. System.out.println("あなたの手は" + you + "です");
  24. cpu = random.nextInt(3); // cpuは0~2
  25. System.out.println("ジャンケン、ポン!\n");
  26. for (long i = 1; i <= 2_000_000_000L; i++) { ; }
  27. System.out.println("コンピュータの手は" + cpu + "です");
  28. if (you == cpu) {
  29. System.out.println("あいこです\n");
  30. } else if ((you + 1) % 3 == cpu) { // youとcpu = 0と1 1と2 2と0
  31. System.out.println("あなたの勝ちです!\n");
  32. isPlay = decide();
  33. } else {
  34. System.out.println("残念、あなたの負けです…\n");
  35. isPlay = decide();
  36. }
  37. }
  38. }
  39. public static boolean decide() {
  40. boolean isPlay;
  41. int res;
  42. Scanner scanner = new Scanner(System.in);
  43.  
  44. while (true) {
  45. System.out.println("続けますか? Yesなら1、Noなら0を入力して下さい");
  46. res = scanner.nextInt();
  47. if (res == 1) {
  48. return true;
  49. } else if (res == 0) {
  50. return false;
  51. } else {
  52. System.out.println("0か1を入力して下さい\n");
  53. }
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement