From 8e29f0bc09e9913b6e94f3257bcf93769273b312 Mon Sep 17 00:00:00 2001 From: htabuchi Date: Wed, 2 Jul 2025 15:36:38 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E2=80=9Cpaiza=E3=81=AE=E5=95=8F=E9=A1=8CC1?= =?UTF-8?q?44=E3=81=AE=E5=9B=9E=E7=AD=94=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htabuchi/src/C144_htabuchi.java | 101 ++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 htabuchi/src/C144_htabuchi.java diff --git a/htabuchi/src/C144_htabuchi.java b/htabuchi/src/C144_htabuchi.java new file mode 100644 index 0000000..7d0319a --- /dev/null +++ b/htabuchi/src/C144_htabuchi.java @@ -0,0 +1,101 @@ +/** + * Paiza問題C144:じゃんけんの結果
+ * create 2025/07/02 + * + * @author 田渕日奈子 + * @version 1.0 + */ + +import java.util.Scanner; + +public class C144_htabuchi { + + public static Dto scanningRequiredValue() { + /** 両者のじゃんけん結果を読み取り */ + Scanner sc = new Scanner(System.in); + int countGame = sc.nextInt(); + + String[] aliceArray = new String[countGame]; + String[] bobArray = new String[countGame]; + + for (int i = 0; i < countGame; i++) { + String handAlice = sc.next(); + String handBob = sc.next(); + + aliceArray[i] = handAlice; + bobArray[i] = handBob; + } + Dto dto = new Dto(countGame, aliceArray, bobArray); + + sc.close(); + return dto; + } + + private static class Dto { + /** じゃんけんデータの読みだし・書き出し */ + private int countGame; + private String[] handAlice; + private String[] handBob; + + public Dto(final int countGame, final String[] handAlice, final String[] handBob) { + this.countGame = countGame; + this.handAlice = handAlice; + this.handBob = handBob; + } + + public int getCountGame() { + return countGame; + } + + public String getHandAlice(int i) { + return handAlice[i]; + } + + public String getHandBob(int i) { + return handBob[i]; + } + + } + + public static int judgeGameWin(Dto dto) { + /** Aliceが勝った回数のみを求める */ + int countAliceWin = 0; + for (int i = 0; i < dto.getCountGame(); i++) { + switch (dto.getHandAlice(i)) { + case "G": + if (dto.getHandBob(i).equals("C")) { + countAliceWin=increaseCount(countAliceWin); + } + break; + case "C": + if (dto.getHandBob(i).equals("P")) { + countAliceWin=increaseCount(countAliceWin); + } + break; + case "P": + if (dto.getHandBob(i).equals("G")) { + countAliceWin=increaseCount(countAliceWin); + } + break; + default: + break; + } + } + return countAliceWin; + } + + public static int increaseCount(int countAliceWin) { + return ++countAliceWin; + } + + public static void displayWinCount(int countAliceWin) { + /** 表示のみの役割 */ + System.out.println(countAliceWin); + } + + public static void main(String[] args) { + Dto dto = scanningRequiredValue(); + int countAliceWin = judgeGameWin(dto); + displayWinCount(countAliceWin); + } +} -- GitLab From 08afe70c5badbb3d39295a54565c3934013de838 Mon Sep 17 00:00:00 2001 From: htabuchi Date: Thu, 3 Jul 2025 15:31:51 +0900 Subject: [PATCH 2/2] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CC144?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htabuchi/src/C144_htabuchi.java | 52 ++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/htabuchi/src/C144_htabuchi.java b/htabuchi/src/C144_htabuchi.java index 7d0319a..9944cb1 100644 --- a/htabuchi/src/C144_htabuchi.java +++ b/htabuchi/src/C144_htabuchi.java @@ -10,7 +10,7 @@ import java.util.Scanner; public class C144_htabuchi { - public static Dto scanningRequiredValue() { + public JankenResultData scanningRequiredValue() { /** 両者のじゃんけん結果を読み取り */ Scanner sc = new Scanner(System.in); int countGame = sc.nextInt(); @@ -25,19 +25,19 @@ public class C144_htabuchi { aliceArray[i] = handAlice; bobArray[i] = handBob; } - Dto dto = new Dto(countGame, aliceArray, bobArray); + JankenResultData dto = new JankenResultData(countGame, aliceArray, bobArray); sc.close(); return dto; } - private static class Dto { + private class JankenResultData { /** じゃんけんデータの読みだし・書き出し */ private int countGame; private String[] handAlice; private String[] handBob; - public Dto(final int countGame, final String[] handAlice, final String[] handBob) { + public JankenResultData(final int countGame, final String[] handAlice, final String[] handBob) { this.countGame = countGame; this.handAlice = handAlice; this.handBob = handBob; @@ -57,24 +57,28 @@ public class C144_htabuchi { } - public static int judgeGameWin(Dto dto) { + public static int[] judgeGameWin(JankenResultData dto) { /** Aliceが勝った回数のみを求める */ - int countAliceWin = 0; + int[] countAliceWin = new int[dto.getCountGame()]; + final String PA = "P"; + final String CHOKI = "C"; + final String GOO = "G"; + for (int i = 0; i < dto.getCountGame(); i++) { switch (dto.getHandAlice(i)) { - case "G": - if (dto.getHandBob(i).equals("C")) { - countAliceWin=increaseCount(countAliceWin); + case GOO: + if (dto.getHandBob(i).equals(CHOKI)) { + countAliceWin[i] = 1; } break; - case "C": - if (dto.getHandBob(i).equals("P")) { - countAliceWin=increaseCount(countAliceWin); + case CHOKI: + if (dto.getHandBob(i).equals(PA)) { + countAliceWin[i] = 1; } break; - case "P": - if (dto.getHandBob(i).equals("G")) { - countAliceWin=increaseCount(countAliceWin); + case PA: + if (dto.getHandBob(i).equals(GOO)) { + countAliceWin[i] = 1; } break; default: @@ -84,8 +88,14 @@ public class C144_htabuchi { return countAliceWin; } - public static int increaseCount(int countAliceWin) { - return ++countAliceWin; + public static int countWinNum(int[] countAliceWin) { + int countWin = 0; + for(int i :countAliceWin) { + if (i==1) { + countWin++; + } + } + return countWin; } public static void displayWinCount(int countAliceWin) { @@ -94,8 +104,10 @@ public class C144_htabuchi { } public static void main(String[] args) { - Dto dto = scanningRequiredValue(); - int countAliceWin = judgeGameWin(dto); - displayWinCount(countAliceWin); + C144_htabuchi program = new C144_htabuchi(); + JankenResultData dto = program.scanningRequiredValue(); + int[] countAliceWin = judgeGameWin(dto); + int countWin = countWinNum(countAliceWin); + displayWinCount(countWin); } } -- GitLab