Last time I posted about variables in Java. Please see that article if you like. What I learned in Java (Part 2) What are variables
This time I would like to write about the statement of instruction execution.
An instruction execution statement is an instruction statement prepared by Java from the beginning. As a basic form of instruction execution statement
It will be. * Information in parentheses is called an argument (hikisu) or parameter.
As one of the concrete ones, "System.out.println ()" used in previous articles etc.
public class Main {
public static void main(String[] args) {
System.out.println("Hello");
}
}
Execution result This is a statement to display the arguments enclosed in parentheses on the screen. There is also a difference between "System.out.println ();" and "System.out.print ();".
public class Main {
public static void main(String[] args) {
//new line
System.out.println("Nice to meet you");
System.out.println("Hello");
//No line breaks
System.out.print("Nice to meet you");
System.out.print("Hello");
}
}
Execution result It is also possible to use with variables
public class Main {
public static void main(String[] args) {
//Variable declaration, assignment
int a = 1;
String b = "Hello";
//Display variables
System.out.println(a);
System.out.println(b);
}
}
Execution result
Instructions for comparing two values
int m = Math.max(a,b);
This is an instruction to compare "a" and "b" specified by the argument and assign the larger one to the variable "m".
public class Main {
public static void main(String[] args) {
//Variable declaration, assignment
int a = 1;
int b = 2;
//Substitute the larger of a and b into the variable m
int m = Math.max(a, b);
//Show assigned value
System.out.println(m);
}
}
Execution result
Converts a number entered in a String type variable to an integer
int seisu = 1;
String moziretu = "1";
The contents "1" of the two variables created above are completely different. Integers cannot be calculated with the character string "1" assigned to the String type. Therefore, the statement for converting a String type number to an int type (integer) is as follows.
int n = Integer.parseInt(moziretu);
Example
public class Main {
public static void main(String[] args) {
//Substitute "1" for each of int type and String type
int seisu = 1;
String moziretu = "1";
//Display by adding int type and String type
System.out.println(seisu + moziretu);
//Convert String type to int type
int n = Integer.parseInt(moziretu);
//After conversion, calculate and display between int types
System.out.println(seisu + n);
}
}
Execution result As you can see from the execution result, the first line is the state where the integer "1" and the character string "1" are lined up side by side, and the second line converts the character string "1" to an integer and the calculation result between the integers. Is displayed.
Instructions to generate random numbers like dice
int r = new java.util.Random().nextInt(10);
In the above, a random number from 0 to 9 is assigned to the variable r. Example
public class Main {
public static void main(String[] args) {
//0 for int type r~Generate and substitute random numbers up to 9
int r = new java.util.Random().nextInt(10);
//Display the variable r
System.out.println(r);
}
}
Execution result (randomly generated numbers are assigned) It is also possible to range.
public class Main {
public static void main(String[] args) {
//0 for int type r~Generate and substitute random numbers up to 9
int r = new java.util.Random().nextInt(10) + 10;
//Display the variable r
System.out.println(r);
}
}
Execution result (random numbers from 10 to 19 are assigned)
Accepts input of strings or integers from the keyboard
//Receive a string
String input1 = new java.util.Scanner(System.in).nextLine();
//Receive an integer
int input2 = new java.util.Scanner(System.in).nextInt();
Example
public class Main {
public static void main(String[] args) {
System.out.println("Please enter the product you purchased");
String input1 = new java.util.Scanner(System.in).nextLine();
System.out.println("Please enter the number of purchases");
int input2 = new java.util.Scanner(System.in).nextInt();
System.out.println(input1 + "To" + input2 + "It's an individual. Thank you for your purchase.");
}
}
Execution result I tried to summarize the basic instruction execution statements.
https://book.impress.co.jp/books/1113101090
Next time, I would like to write about conditional branching and repetition.
Recommended Posts