Let's do a simple exercise using the knowledge so far.
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.
Please change "rock-paper-scissors that only produces goo" to rock-paper-scissors that only produces choki.
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.
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.
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)
About the whole program As you can see in the flowchart, this time we will only consider the case of winning or losing. Please implement according to the specifications without being bound by this common sense.
①
Apply numbers to goo, choki, and par. If you don't apply it, the program will work, but when others read it, you won't know what each number refers to. Numbers that are meaningful but you don't know what they are referring to are called magic numbers </ font>, and if you write this way, you will continue to complain.
The values of goo, choki, and par this time will not be changed, so add final
in front of the type to make it a constant </ b>.
If you make it a constant, you cannot rewrite the value. Please rewrite it as a trial.
Unlike variables, constants are written in all uppercase </ b>.
② Set your own hand and the enemy's hand. Write variables in camel letters (mix case like camel back). This time, you can write solid in the source code.
③ Make a condition judgment. The value is calculated in the if minute, but basically it is not good to put the calculation formula directly in the conditional expression ... However, this time it is a disposable sample code with only one branch. .. ..
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.
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.
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