I first touched Java ④

I tried to touch Java

Please forgive me as it is a self-sufficient memorandum

Try to make various

Continuing from the previous session.

④ 4-choice quiz application

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.

bonus

① ArrayList and array are different

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

I first touched Java ②
I first touched Java ③
I first touched Java ④
I first touched Java
I touched Scala
First gradle build (Java)
I touched Scala ~ [Class] ~
I touched on the new features of Java 15
I touched Scala ~ [Object] ~
I touched Scala ~ [Trait] ~
After all, if you learn first, I think Java
What I researched about Java 8
I started Java Gold (Chapter 1-1)
What I researched about Java 6
I made roulette in Java.
What I researched about Java 9
I investigated Java primitive types
I touched Scala ~ [Control syntax] ~
I took Java SE8 Gold.
I tried Drools (Java, InputStream)
What I researched about Java 7
I tried using Java REPL
[Java] I implemented the combination.
First Java development in Eclipse
Java concurrency I don't understand
I studied the constructor (java)
I tried metaprogramming in Java
What I researched about Java 5
JAVA (First step: Git Bush edition)
I sent an email in Java
I compared PHP and Java constructors
I created a PDF in Java.
I made a shopify app @java
I checked Java Flight Recorder (JFR)
I fell into Java Silver (crying)
I tried to interact with Java
I tried UDP communication with Java
I wrote Goldbach's theorem in java
I tried the Java framework "Quarkus"
Java
I tried using Java8 Stream API
What I learned with Java Gold
I made an annotation in Java.
I tried using JWT in Java
I tried to summarize Java learning (1)
Java
What I learned with Java Silver
What I researched about Java learning
I tried to summarize Java 8 now
<First post> I started studying Ruby
I tried using Java memo LocalDate
I compared Java and Ruby's FizzBuzz.
I tried using GoogleHttpClient of Java
I touched Tribuo published by Oracle. Document Tribuo --A Java prediction library (v4.0)
[Java] I participated in ABC-188 of Atcorder.
I tried using Elasticsearch API in Java
Introduction to java for the first time # 2
First steps for deep learning in Java
Java for All! I read everyone's Java #minjava
I tried to summarize Java lambda expressions
Java9 was included, so I tried jshell.