Something about java

For myself

Approximate template


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

Something about java
About Java interface
[Java] About Java 12 features
[Java] About arrays
Where about java
About Java features
About Java threads
[Java] About interface
About Java class
About Java arrays
About java inheritance
About interface, java interface
About List [Java]
About java var
About Java literals
About Java commands
About Java log output
About Java functional interface
Java, about 2D arrays
About class division (Java)
About [Java] [StreamAPI] allMatch ()
About Java StringBuilder class
[Java] About Singleton Class
About Java method binding
[Java] About anonymous classes
About method splitting (Java)
[Java Silver] About initialization
About Java Array List
About Java Polymorphism super ()
About inheritance (Java Silver)
About Java String class
About Java access modifiers
About Java lambda expressions
About Java entry points
About Java 10 Docker support
Personal summary about Java
[Java] About enum type
All about Java programming
About java abstract class
A note about Java GC
What I researched about Java 8
About an instance of java
[Gradle] About Java plug-in tasks
About Java variable declaration statements
[Java] About try-catch exception handling
About Java class loader types
[Java Silver] About equals method
[Java] About String and StringBuilder
Java
What I researched about Java 7
About =
About Alibaba Java Coding Guidelines
About Java class variables class methods
About Java Packages and imports
About abstract classes in java
Java
[Android / Java] Learned about DataBinding
What I researched about Java 5
How about TECH ACADEMY ?? [Java course]
[Introduction to Java] About lambda expressions
About fastqc of Biocontainers and Java