I first touched Java

I tried to touch Java

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

Display a string

class Hello {
  public static void main(String[] args) {
    System.out.println("hello IKEMEN");
  }
}

Terminal


hello IKEMEN

The character string enclosed in "" is displayed.

Display a string using a variable

class Hello {
  public static void main(String[] args) {
    String name = "IKEMEN"

    System.out.println("hello" + name);
  }
}

Terminal


helloIKEMEN

There is no margin as it is, so I will correct it.

class Hello {
  public static void main(String[] args) {
    String name = "IKEMEN"

    System.out.println("hello " + name);
  }
}

Put a margin after hello in System.out.println ("hello" + name) ;. ↓

Terminal


hello IKEMEN

There is a margin and it is beautiful.

Receive input from users

① Character string

import java.util.Scanner;

class Hello {
  public static void main(String[] args) {
    System.out.print("Your name? ");
    String name = new Scanner(System.in).next();

    System.out.println("hello " + name);
  }
}

It seems that new Scanner (System.in) .next (); can receive input from the keyboard. ↓

Terminal


Your name?

Is displayed, so type in the character string (IKEMEN) ↓

Terminal


Your name? IKEMEN
hello IKEMEN

② Numerical value

import java.util.Scanner;

class Hello {
  public static void main(String[] args) {
    System.out.print("Your guess? ");
    Integer guess = new Scanner(System.in).nextInt();
    
    System.out.println("Your guess: " + guess);
  }
}

It seems that new Scanner (System.in) .nextInt (); receives the input value as an integer. ↓

Terminal


Your guess?

Is displayed, so enter the numerical value (6). ↓

Terminal


Your guess? 6
Your guess: 6

Change the display according to the conditions

import java.util.Scanner;

class Hello {
  public static void main(String[] args) {
    Integer answer = 6;

    System.out.print("Your guess? ");
    Integer guess = new Scanner(System.in).nextInt();
    
    if (answer == guess) {
      System.out.println("Correct answer!");
    } else {
      System.out.print("Lost");
    }
  }
}

If the entered number is 6, it will be displayed as correct! If it is not 6, it will be displayed as lost.

Add a condition

import java.util.Scanner;

class Hello {
  public static void main(String[] args) {
    Integer answer = 6;

    System.out.print("Your guess? ");
    Integer guess = new Scanner(System.in).nextInt();
    
    if (answer == guess) {
      System.out.println("Correct answer!");
    } else if (answer > guess) {
      System.out.println("Larger");
    } else {
      System.out.println("Smaller");
    }
  }
}

If the number you entered is 6, it will be displayed as correct !, if it is less than 6, it will be displayed as larger, and if it is greater than 6, it will be displayed as smaller.

Recommended Posts

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
[day: 5] I summarized the basics of Java