: 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:
: 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
: 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
: 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:
: 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
: 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