[Java] Basic statement for beginners

System.out.print (characters, numbers, etc. you want to display)

: sunny: Display characters on the screen without line breaks

If you create it with System.out.println, which you usually use ...

Example.java


public class Main {
	public static void main(String[] args) {
	    String animal = "Is it";
		System.out.println("My favorite animal is");
		System.out.println(animal);
		System.out.println("is");
	}
}

console.


My favorite animal is
Is it
is

It is output with line breaks like this. System.out.println → Change to System.out.print and try to output ...

Example.java


public class Main {
	public static void main(String[] args) {
	    String animal = "Is it";
		System.out.print("My favorite animal is");
		System.out.print(animal);
		System.out.print("is");
	}
}

console.


Is there any animal I like

Displayed in one line: laughing:

int variable Math.max (①, ②);

: sunny: Compare ① and ② and call the larger one

Example.java


public class Main {
	public static void main(String[] args) {
	    int numberA = 3;
	    int numberB = 8;
	    int numberC = Math.max(numberA,numberB);
		System.out.println(numberC);
	}
}

On line 3, assign 3 to the variable numberA. On line 4, assign 8 to the variable numberB. On line 5, assign Math.max (numberA, numberB); to the variable numberC. Math.max means that the larger one is assigned by comparison, so 3 of numberA and 8 of numberB are compared and the larger one is assigned to numberC.

console.


8

int variable = Integer.parseInt (string you want to convert to numbers);

: sunny: Convert letters to numbers

Example.java


public class Main {
	public static void main(String[] args) {
	    String number = "1";
	    int money = Integer.parseInt(number);
		System.out.println(money + "I want 100 million yen");
	}
}

On the third line, assign the string 1 to the variable number. In line 4, assign the string-to-number 1 to the variable money.

console.


I want 100 million yen

int variable = new java.util.Random (). nextInt (upper limit of random numbers);

: sunny: Generate a random number (randomly retrieve a different value each time) : sunny: The upper limit of random numbers does not include the specified value

Example.java


public class Main {
	public static void main(String[] args) {
	    int number = new java.util.Random().nextInt(6);
		System.out.println(Number);
	}
}

Enter a statement to generate a random number on the third line. I want you to be a little careful here This is the part of nextInt (6) ;. I wrote that [the upper limit of random numbers does not include the specified value], but this is the case.

The number 6 is entered in the parentheses. This means that numbers from 0 to 5 will be displayed randomly. Not including the specified value means not including 6.

If you want to randomly display the numbers from 1 to 5 by other methods

Example.java


int number = new java.util.Random().nextInt(5) + 1;

Or

Example.java


int number = new java.util.Random().nextInt(5);
number++;

Randomly displays numbers from 1 to 5: smile:

String variable = new java.util.Scanner (System.in) .nextLine ();

: sunny: You can enter characters from the keyboard

Example.java


public class Main {
	public static void main(String[] args) {
	    System.out.println("Please enter your name");
	    String name = new java.util.Scanner(System.in).nextLine();
		System.out.println("Hello" + name + "Mr.");
	}
}

You will be prompted to enter your name on the third line. You will be able to type characters on the 4th line, so enter any name you like. Assign the entered name to the variable name.

console.


Please enter your name
Miki
Hi Miki's

int variable = new java.util.Scanner (System.in) .nextInt ();

: sunny: You can enter integers from the keyboard

Example.java


public class Main {
	public static void main(String[] args) {
	    System.out.println("What day is it today?");
	    int day = new java.util.Scanner(System.in).nextInt();
		System.out.println("today" + day + "It's a day");
	}
}

On the third line, what day is it today? It will be displayed. You can enter an integer on the 4th line, so enter your favorite integer. Substitute the date entered in the 4th line into the variable day.

console.


What day is it today?
29
Today is the 29th

Recommended Posts

[Java] Basic statement for beginners
Java for statement
Complex for statement (basic)
[Java] for statement / extended for statement
(Memo) Java for statement
Java debug execution [for Java beginners]
Java for beginners, data hiding
Java application for beginners: stream
Java (while statement) basic problem that beginners did first
Introduction to Java for beginners Basic knowledge of Java language ①
[For beginners] Summary of java constructor
Rock-paper-scissors game for beginners in Java
Java for beginners, expressions and operators 1
[For beginners] Run Selenium in Java
Java for beginners, expressions and operators 2
[For Java beginners] About exception handling
Classes and instances Java for beginners
Let's learn SQL SELECT statement for beginners
For JAVA learning (2018-03-16-01)
Java basic grammar
Java basic grammar
Learn Java with "So What" [For beginners]
[For beginners] Difference between Java and Kotlin
Java basic knowledge 1
[Java] Basic structure
[Java] [Basic] Glossary
2017 IDE for Java
Java basic grammar
Java basic grammar
Java, for statement / while statement starting from beginner
Java exercises [Basic]
Java switch statement
Use Java7 try-with-resources statement for Cursor close processing
Kantai Collection Java # 1 Classes and Objects [For Beginners]
A collection of simple questions for Java beginners
[Introduction to Java] Basics of java arithmetic (for beginners)
Let's use Java New FileIO! (Introduction, for beginners)
[For beginners] Introduction to Java Basic knowledge of Java language ③ Array, selection structure, iteration structure
Java instruction execution statement
[Java] Package for management
java basic knowledge memo
[Java] Data type ①-Basic type
Countermeasures for Java OutOfMemoryError
Java basic date manipulation
Java basic naming conventions
NLP for Java (NLP4J) (2)
Java learning memo (basic)
NLP for Java (NLP4J) (1)
[Java] Basic method notes
Java basic data types
Basic Java OOps concepts
Scraping for beginners (Ruby)
[Java] Let's create a mod for Minecraft 1.14.4 [0. Basic file]
[For beginners] Quickly understand the basics of Java 8 Lambda
[For beginners] How to operate Stream API after Java 8
[Java] Let's create a mod for Minecraft 1.16.1 [Basic file]
[Java basics] Let's make a triangle with a for statement
[For beginners] Minimum sample to display RecyclerView in Java
Java development for beginners to start from 1-Vol.1-eclipse setup
I tried using an extended for statement in Java
List of frequently used Java instructions (for beginners and beginners)