Rock-paper-scissors game for beginners in Java

It's been about a month since I started studying, so I wrote a rock-paper-scissors game while reviewing. I was able to write a program with only the main method relatively quickly, I had a hard time trying to make something that looked like object-oriented. Since I am self-taught, I think there are many things to do.



package janken;

public class Main {
    public static void main(String[] args) {
        final String errorException = "Please enter the correct number.";
        final String errorMessage = "Please enter a number from 0 to 2 or 99.";
        final String exitMessage = "Finish";
		final String menuMessage = "0:Goo 1:Choki 2:Par 99:End";
		Player you = new Player();
		Player enemy = new Player();
		for(;;) {
		    try {
		    	you.jankenNum = new java.util.Scanner(;	
			    enemy.jankenNum = new java.util.Random().nextInt(3);
			    if(you.jankenNum == 99) {
			    } else if(you.jankenNum < 0 | you.jankenNum > 2) {
			    	//If an incorrect number is entered ...
			    } else {
			    	//Battle method call.
			    	Battle result = new Battle();
			    	result.battle(you, enemy);
		    } catch(java.util.InputMismatchException error) {
		    	//If a non-numeric value is entered ...


package janken;

public class Player {
	//Holds the number of wins and losses of the player. Defines rock-paper-scissors hands, wins and losses, etc.
    int jankenNum;
    final String goo = "Goo";
    final String choki = "Choki";
    final String par = "Par";
    int win; int lose; int draw;
    String Hand() {
    	//Convert player numbers into rock-paper-scissors hands.
		switch(this.jankenNum) {
		case 0:
		case 1:
		    return this.choki;
		case 2:
		    return this.par;
			return null;
    void board(int win, int lose, int draw) {
    	//Display the number of wins and losses.
    	System.out.println("Win:" + + " Lose:" + this.lose + " Draw:" + this.draw);


package janken;

public class Battle {
	//Decide the victory or defeat.
	final String aiko = "Aiko";
	final String kachi = "win";
	final String make = "Lose";
	void battle(Player pc1, Player pc2) {
		//Decide whether to win or lose between players.
		int result = (pc1.jankenNum - pc2.jankenNum +3) % 3;
	    switch(result) {
	    //Win or lose is decided from each other's hands and stored as a score. View results.
	    case 0:
	    	System.out.println(pc1.Hand() + " VS " + pc2.Hand());
	    	pc1.draw += 1; pc2.draw += 1;
	    	pc1.board(, pc1.lose, pc1.draw);
	    case 1:
	    	System.out.println(pc1.Hand() + " VS " + pc2.Hand());
	    	pc1.lose += 1; += 1;
	    	pc1.board(, pc1.lose, pc1.draw);
	    case 2:
	    	System.out.println(pc1.Hand() + " VS " + pc2.Hand());
	    	System.out.println(kachi); += 1; pc2.lose += 1;
	    	pc1.board(, pc1.lose, pc1.draw);

I'm satisfied because it worked like that for the time being. It's quite difficult to think about how to divide it.

Recommended Posts

Rock-paper-scissors game for beginners in Java
[For beginners] Run Selenium in Java
Rock-paper-scissors in Java
Rock-paper-scissors game java practice
I made a rock-paper-scissors game in Java (CLI)
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
Java for beginners, data hiding
Java application for beginners: stream
[For beginners] Minimum sample to display RecyclerView in Java
[For beginners] Explanation of classes, instances, and statics in Java
Java for beginners, expressions and operators 1
Java for beginners, expressions and operators 2
Rock-paper-scissors game
Age guessing game made in Java
Settings for SSL debugging in Java
[For Java beginners] About exception handling
java rock-paper-scissors
Classes and instances Java for beginners
A rock-paper-scissors game for two players who play against each other in threads with Java
[For beginners] Minimum sample to update RecyclerView with DiffUtils in Java
First steps for deep learning in Java
Key points for introducing gRPC in Java
Learn Java with "So What" [For beginners]
[Java] for Each and sorted in Lambda
[For beginners] Difference between Java and Kotlin
[For beginners] How to debug in Eclipse
For JAVA learning (2018-03-16-01)
Partization in Java
Changes in Java 11
2017 IDE for Java
I wrote EX25 of AtCoder Programming Guide for beginners (APG4b) in java.
Pi in Java
[Java] [For beginners] How to insert elements directly in a 2D array
Java for statement
FizzBuzz in Java
[For beginners] I tried using DBUnit in Eclipse
ChatWork4j for using the ChatWork API in Java
Technology for reading Java source code in Eclipse
[For beginners] I tried using JUnit 5 in Eclipse
Solution for NetBeans 8.2 not working in Java 9 environment
Kantai Collection Java # 1 Classes and Objects [For Beginners]
A collection of simple questions for Java beginners
Set pop-up display for Java language in vim.
[Introduction to Java] Basics of java arithmetic (for beginners)
Let's use Java New FileIO! (Introduction, for beginners)
[Java beginner's anguish] Hard-to-test code implemented in Junit
Compare PDF output in Java for snapshot testing
Enable / disable SNI in Java for each communication
Things to watch out for in Java equals
[java] sort in list
[Java] for statement, while statement
Read JSON in Java
Make Blackjack in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
[For beginners] DI ~ The basics of DI and DI in Spring ~