I first touched Java ③

I tried to touch Java

Please forgive it as it will be a self-sufficient memorandum.

Try to make various

I would like to make use of my knowledge to create functions for Hiroshima Toyo Carp, which is a big fan.

① Display the term of office of the director

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

② 4-choice quiz

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!

③ Application of 4-choice quiz

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

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 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)
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 Cassandra's Object Mapper for Java
I tried to summarize Java lambda expressions
Java9 was included, so I tried jshell.
I tried the new era in Java
Java SE 8 Silver (Java SE 8 Programmer I) Pass Note