Please forgive me as it is a self-sufficient memorandum
Continuing from the previous session.
Improvements to this feature that I considered last time ① 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.
I will start from the place where I can do it for the time being. ② will be improved.
Test4.java
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
class Test4 {
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");
for (int i = 0; i <= 3; i++) {
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!");
break;
} else {
array.remove(number - 1);
System.out.println("That's wrong");
System.out.println("Please reselect the number.");
}
}
}
}
Terminal
What university did Morishita, who ranked first in the 2019 draft, come from?
[[1]Meiji University, [3]Waseda University, [4]Rikkyo University]
1
Is the correct answer!
↑ Correct answer pattern I wrote break ;, so the repetition stopped.
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.
What university did Morishita, who ranked first in the 2019 draft, come from?
[[1]Meiji University, [3]Waseda University, [4]Rikkyo University]
1
Is the correct answer!
↑ Correct pattern by mistake once It's a good move.
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]
3
That's wrong
Please reselect the number.
What university did Morishita, who ranked first in the 2019 draft, come from?
[[1]Meiji University, [2]Hosei University, [4]Rikkyo University]
4
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:359)
at java.base/java.util.ArrayList.remove(ArrayList.java:504)
at Test4.main(Test4.java:23)
↑ There was a problem. If you select 4 after making a mistake, there will be a discrepancy between the number (2) on the array and the entered number (3) because it is the third number on the array.
This correction is difficult with the current knowledge, so it is painful, but I made it like this.
Test4.java
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
class Test4 {
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");
for (int i = 0; i <= 3; i++) {
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!");
break;
} else {
array.set(number - 1, "");
System.out.println("That's wrong");
System.out.println("Please reselect the number.");
}
}
}
}
I was removing the element with array.remove (number -1) ;, By making a blank with array.set (number --1, "") ;, the deviation of the number of elements was eliminated.
Terminal
[[1]Meiji University, [2]Hosei University, [3]Waseda University, [4]Rikkyo University]
2
That's wrong
Please reselect the number.
What university did Morishita, who ranked first in the 2019 draft, come from?
[[1]Meiji University, , [3]Waseda University, [4]Rikkyo University]
3
That's wrong
Please reselect the number.
What university did Morishita, who ranked first in the 2019 draft, come from?
[[1]Meiji University, , , [4]Rikkyo University]
4
That's wrong
Please reselect the number.
What university did Morishita, who ranked first in the 2019 draft, come from?
[[1]Meiji University, , , ]
1
Is the correct answer!
It's not ideal, but let's do it!
Next, improve ③.
Test5.java
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
class Test5 {
public static void main(String args[]) {
List<String> array01 = new ArrayList<String>();
array01.add("[1]Meiji University");
array01.add("[2]Hosei University");
array01.add("[3]Waseda University");
array01.add("[4]Rikkyo University");
for (int i = 0; i <= 3; i++) {
System.out.println("What university did Morishita, who ranked first in the 2019 draft, come from?");
System.out.println(array01);
Integer number = new Scanner(System.in).nextInt();
if (number == 1) {
System.out.println("Is the correct answer!");
break;
} else {
array01.set(number - 1, "");
System.out.println("That's wrong");
System.out.println("Please reselect the number.");
}
}
List<String> array02 = new ArrayList<String>();
array02.add("[1]Meiji University");
array02.add("[2]Hosei University");
array02.add("[3]Toyo University");
array02.add("[4]Asia University");
for (int i = 0; i <= 3; i++) {
System.out.println("What university did Ugusa, who is second in the draft in 2019, come from?");
System.out.println(array02);
Integer number = new Scanner(System.in).nextInt();
if (number == 2) {
System.out.println("Is the correct answer!");
break;
} else {
array02.set(number - 1, "");
System.out.println("That's wrong");
System.out.println("Please reselect the number.");
}
}
List<String> array03 = new ArrayList<String>();
array03.add("[1]Hanasaki Tokuharu High School");
array03.add("[2]Tsuruga Kibi High School");
array03.add("[3]Kasumigaura High School");
array03.add("[4]Ofunato High School");
for (int i = 0; i <= 3; i++) {
System.out.println("What high school did Suzuki, who is third in the 2019 draft, come from?");
System.out.println(array03);
Integer number = new Scanner(System.in).nextInt();
if (number == 3) {
System.out.println("Is the correct answer!");
break;
} else {
array03.set(number - 1, "");
System.out.println("That's wrong");
System.out.println("Please reselect the number.");
}
}
}
}
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.
What university did Morishita, who ranked first in the 2019 draft, come from?
[[1]Meiji University, , [3]Waseda University, [4]Rikkyo University]
1
Is the correct answer!
What university did Ugusa, who is second in the draft in 2019, come from?
[[1]Meiji University, [2]Hosei University, [3]Toyo University, [4]Asia University]
1
That's wrong
Please reselect the number.
What university did Ugusa, who is second in the draft in 2019, come from?
[, [2]Hosei University, [3]Toyo University, [4]Asia University]
3
That's wrong
Please reselect the number.
What university did Ugusa, who is second in the draft in 2019, come from?
[, [2]Hosei University, , [4]Asia University]
2
Is the correct answer!
What high school did Suzuki, who is third in the 2019 draft, come from?
[[1]Hanasaki Tokuharu High School, [2]Tsuruga Kibi High School, [3]Kasumigaura High School, [4]Ofunato High School]
3
Is the correct answer!
For the time being, I tried arranging three of the same functions. There is no problem as a function. However, I think it can be made a little more compact. maybe.
I found this out as I investigated. At first I thought it was the same. 【the difference】 -ArrayList has a variable size (number of elements) ・ The declaration method is different -ArrayList does not contain primitive types Primitive type: Value type such as int or boolean
Recommended Posts