Please forgive it as it will be a self-sufficient memorandum.
I would like to make use of my knowledge to create functions for Hiroshima Toyo Carp, which is a big fan.
Select a number and make it a function to retrieve from the array.
Test.java
import java.util.Scanner;
class Test {
public static void main(String args[]) {
System.out.println("[1]Director Tatsukawa\n[2]Director Yamamoto\n[3]Director Brown\n[4]Director Nomura\n[5]Director Ogata\n[6]Director Sasaoka\Select a number from n1 to 6.");
Integer number = new Scanner(System.in).nextInt();
String[] array = {"the year of 2000", "2001-2005", "2006-2009", "2010-2014", "2015-2019", "2020"};
System.out.println(array[number - 1]);
}
}
Terminal
[1]Director Tatsukawa
[2]Director Yamamoto
[3]Director Brown
[4]Director Nomura
[5]Director Ogata
[6]Director Sasaoka
Please select a number from 1 to 6.
4
2010-2014
Please note that I forgot to write import java.util.Scanner; and the Scanner could not be used. ↓ Error statement
Terminal
Test.java:4:error:Can't find symbol
Integer number = new Scanner(System.in).nextInt();
^
symbol:Class Scanner
place:Class Test
Select a number from 4 choices and make a simple conditional branch.
Test2.java
import java.util.Scanner;
class Test2 {
public static void main(String args[]) {
System.out.println("What university did Morishita, who ranked first in the 2019 draft, come from?\n[1]Meiji University\n[2]Hosei University\n[3]Waseda University\n[4]Rikkyo University");
Integer number = new Scanner(System.in).nextInt();
if (number == 1) {
System.out.println("Is the correct answer!");
} else {
System.out.println("That's wrong");
}
}
}
Terminal
What university did Morishita, who ranked first in the 2019 draft, come from?
[1]Meiji University
[2]Hosei University
[3]Waseda University
[4]Rikkyo University
2
That's wrong
Terminal
What university did Morishita, who ranked first in the 2019 draft, come from?
[1]Meiji University
[2]Hosei University
[3]Waseda University
[4]Rikkyo University
1
Is the correct answer!
If you make a mistake, reduce the options.
Test3.java
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
class Test3 {
public static void main(String args[]) {
List<String> array = new ArrayList<String>();
array.add("[1]Meiji University");
array.add("[2]Hosei University");
array.add("[3]Waseda University");
array.add("[4]Rikkyo University");
System.out.println("What university did Morishita, who ranked first in the 2019 draft, come from?");
System.out.println(array);
Integer number = new Scanner(System.in).nextInt();
if (number == 1) {
System.out.println("Is the correct answer!");
} else {
array.remove(number - 1);
System.out.println("That's wrong");
System.out.println("Please reselect the number.");
System.out.println(array);
}
}
}
import java.util.ArrayList; import java.util.List; If you do not fill in, an error will occur, so please be careful.
Terminal
What university did Morishita, who ranked first in the 2019 draft, come from?
[[1]Meiji University, [2]Hosei University, [3]Waseda University, [4]Rikkyo University]
1
Is the correct answer!
↑ Correct answer pattern
Terminal
What university did Morishita, who ranked first in the 2019 draft, come from?
[[1]Meiji University, [2]Hosei University, [3]Waseda University, [4]Rikkyo University]
2
That's wrong
Please reselect the number.
[[1]Meiji University, [3]Waseda University, [4]Rikkyo University]
↑ Wrong pattern 2 Hosei University is no longer displayed.
For the time being, this article ends like this. Improvements to this feature ① When displaying the contents of the array on the terminal like [[1] Meiji University, [2] Hosei University, [3] Waseda University, [4] Rikkyo University], do not support the display of []. ② Please select again. Allows you to enter numbers when the message is displayed. I think it can be done by iterative processing. ③ Increase the number of problems. ④ Make it possible to ask questions at random. ⑤ Make it possible to display the answers randomly. As far as I can think of, it looks like this. I would like to write it in the next article.
I still don't know about Java, so I'll do my best.
Recommended Posts