For myself
class file name{
public static void main(String[] args) { //Needed for the time being
int number; //Because it is an integer, int
number = 3; //Substitute 3 for number
System.out.println(number); //output method(Substituted guy)
String name; //The string is String
name = "Wanko"; //Substitute wanko for name
System.out.println(name); //output method
}
}
##Basic (how to send a message)
class file name { public static void main(String[] args) { System.out.println ("○○"); // The text here is reflected } }
##variable
class MyApp { public static void main(String[] args) { String name = "taguchi" // The right side is assigned to the left side
System.out.println("hello " + name); //hello + name(taguchi)
System.out.println("hello " + name + " again!"); // hello + name(taguchi) + again
} }
~ $ javac MyApp.java && java MyApp hello taguchi hello taguchi again!
##Receive input from users
import java.util.Scanner;
class MyApp { public static void main(String[] args) { System.out.println ("Your name?"); // Waiting comment There is a line break System.out.print ("Your name?"); // Waiting comment No line breaks String name = new Scanner(System.in).next();
System.out.println("hello " + name);
System.out.println("hello " + name + " again!");
} } The terminal is waiting for a reply and you will receive a reply when you send
##Let's receive the numerical value and calculate
import java.util.Scanner;
class MyApp { public static void main(String[] args) { System.out.print("Your guess? "); Integer guess = new Scanner(System.in).nextInt();
System.out.println("Your gess: " + guess); } } As new Scanner (System.in), if you want to receive an integer value, use nextInt ()
##Let's change the display according to the conditions
import java.util.Scanner;
class MyApp { public static void main(String[] args) { Integer answer = 6; // Answer to the question
System.out.print ("Your guess?"); // Question Integer guess = new Scanner(System.in).nextInt();
if (answer == guess) {
System.out.println ("Bingo!"); // Answer above (correct answer) }else { System.out.println ("Booooooo"); // Answer above (incorrect answer) }
##Addition of above
import java.util.Scanner;
class MyApp { 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 ("Bingo!"); // Answer } else if (answer > guess) { System.out.println ("The answer is higher"); // Meaning higher } else { System.out.println ("The answer is lower"); // Meaning lower } } }
##Let's generate a random number
import java.util.Scanner; import java.util.Random; // Code to make random numbers
class MyApp { public static void main(String[] args) { Integer answer = new Random (). nextInt (10) + 1; // Random code (+1 is 1 10 added)
System.out.print("Your guess? ");
Integer guess = new Scanner(System.in).nextInt();
if (answer == guess) {
System.out.println("Bingo!");
} else if (answer > guess) {
System.out.println("The answer is higher!");
} else {
System.out.println("The answer is lower!");
}
System.out.println("The answer was " + answer);
} }
##Let's use loop processing
import java.util.Scanner; import java.util.Random;
class MyApp { public static void main(String[] args) { Integer answer = new Random().nextInt(10) + 1;
while (true) {// Code for loop processing System.out.print("Your guess? "); Integer guess = new Scanner(System.in).nextInt();
if (answer == guess) {
System.out.println("Bingo!");
break; // Code that prevents looping when the answer is correct } else if (answer > guess ) { System.out.println("The answer is higher!"); } else { System.out.println("The answer is lower!"); } } } }
##Let's display the number of times until the correct answer
import java.util.Scanner; import java.util.Random;
class MyApp { public static void main(String[] args) { Integer answer = new Random().nextInt(10) + 1; Integer count = 0;
while (true) {
System.out.print("Your guess? ");
Integer guess = new Scanner(System.in).nextInt();
count = count + 1;
// count + = 1; // Omitted above // count ++; // Further omit the above
if (answer == guess) {
System.out.println("Bingo! It took " + count + " guesses!");
break; // Code to stop when the answer is correct } else if (answer > guess ) { System.out.println("The answer is higher!"); } else { System.out.println("The answer is lower!"); } } } }
Recommended Posts