Java Development Basics ~ How to Write Programs * Exercise 1 ~

Java Development Basics ~ How to Write Programs * Exercises ~

Let's do a simple exercise using the knowledge so far.

Required knowledge

Problem 1-1

Please refer to the flowchart and implement "rock-paper-scissors that only gives goo" and be sure to implement the program that you win. Please refer to this page for the judgment of winning or losing of rock-paper-scissors. じゃんけん.png

Problem 1-2

Please change "rock-paper-scissors that only produces goo" to rock-paper-scissors that only produces choki.

Problem 1-3

Please change "rock-paper-scissors that only produces goo" to ordinary rock-paper-scissors that produces other than goo. However, in this case, do not repeat and output "Aiko" as a result.

Problem 1-4

Allow your hands to enter values externally and display the results for each match. Create a rock-paper-scissors program that won't end until you win. Also, display the number of times it took to win at the end.


Answer

Problem 1-1

Janken.java


package janken;
public class Gudake {
	public static void main(String[] args) {
		//Variables that do not change once set"final"Add to make it a constant. ・ ・ ・ ①
		final int GUU=0;
		final int CHOKI=1;
		final int PA=2;
		//Set your own hands and the enemy's hands ... ②
		int myHand=PA;
		int enemyHand=CHOKI;
		//Conditional expression
		//Is it better to separate this conditional expression in terms of code? ③
		if(((myHand-enemyHand)%3)==2) {
			System.out.println("You win");
		}else {
			System.out.println("That shouldn't be the case");
		}
	}
}

Commentary)

Problem 1-2

Just change ʻenemyHand = GU in 1-1 to ʻenemyHand = CHOKI. If you haven't made the goo, choki, and par constants, you'll forget which number corresponds to which hand, but if you make them constant like this time, it will be easier to modify the program.

Problem 1-3

Janken.java


package janken;
import java.util.Random;
public class Janken {
	public static void main(String args[]) {
		final int GUU=0;
		final int CHOKI=1;
		final int PA=2;
		int myHand=CHOKI;
		//Randomly set the opponent's hand. ・ ・ ・ ①
        Random roundom = new Random(2);
		int enemyHand=roundom.nextInt(3);
		//When writing with if ... ②
		//Calculate the winning and losing conditions in advance ... ③
		int winningResult=((myHand-enemyHand)%3);
		if(winningResult==0){
			System.out.println("I'm Aiko");
		}else if (winningResult==1) {
			System.out.println("I'm losing");
		}else {
			System.out.println("Win");
		}
		//Place to write with switch ・ ・ ・ 4
		switch((myHand-enemyHand)%3){
			case 0:
				System.out.println("I'm Aiko");
				break;
			case 1:
				System.out.println("I'm losing");
				break;
			case 2:
				System.out.println("Win");
				break;
		}
	}
}
  • About this program It's normal rock-paper-scissors, but be careful not to repeat it in this case. There are wins, losses, and aiko, and there are 3 patterns for judgment.

  • ① I used Random roundom = new Random (2); when generating a random number. If you calculate with java random etc., a lot of sample programs will come out, so let's refer to it. There are many parts in java that can do what you want to do in one line, so search for them as appropriate and use them.

  • ② There are three patterns of branching, win, lose, and aiko, but java can only answer yes no questions. Let's think about the flow so that the yes no problem can cover 3 patterns.

  • ③ The condition is judged multiple times in order to branch in 3 patterns. You can write the judgment formula separately in the if part, but if the judgment formula is incorrect, the number of corrections will increase. Let's cut out the judgment formula outside considering that a bug will occur in the program.

  • ④ In this case, it may be cleaner to write using switch than to use if. I will also describe the case of branching with a switch.

Problem 1-4

It seems that it is necessary to write from the flowchart, and it is a little difficult, so I will add it if requested. .. ..

Recommended Posts