I started studying Java grammar and there are many complicated rules compared to Ruby, so make a note of it.
System.out.println "Java"; //This is the same role as Puts in Ruby at the end of the code;Hit
To define variables in Java (1) Specify the data type of the value to be put in the variable, (2) Decide the name of the variable, Two are required.
int number = 7;
String name = "Sato"; //String type s is uppercase
double syosu = 3.14;
int number1 = 7;
int number2 = 2;
System.out.println(number1 / number2); //The result is 3
System.out.println((double)number1 / number2); //The result is 3.5
System.out.println(12 / 4 == 3); //Output as true
System.out.println(12 / 4 != 3); //output as false
Write the conditional expression in () after if
if (true) {
System.out.println("Output because the conditional expression is true");
}
if (false) {
System.out.println("Not output because the conditional expression is false");
}
int number = 13;
//Processing when it does not match any case using default
switch (number % 5) {
case 0:
System.out.println("I'm Daikichi");
break;
case 1:
System.out.println("I'm Nakayoshi");
break;
case 4:
System.out.println("It ’s bad.");
break;
default:
System.out.println("I'm good");
break;
}
First, specify the type of the elements of the array. The array is {} curly braces.
String names[] = {"YUI","YUKI","YOU"};
String[] names = {"dog", "Cat", "turtle"};
for (String name : names){
System.out.println("My pet"+name+"is");
}
Java executes classes, not files. The flow that there is a Main method in the Main class and the hello method in it is executed.
class Main {
//Method definition void means no return value
public static void main(String[] args) {
hello();
}
public static void hello() {
// "Hello World","Hello Java"Please rewrite to
System.out.println("Hello Java");
}
}
class Main {
public static void main(String[] args) {
//Assign the result of the fullName method to the variable name
String name = fullName("Kate" ,"Jones");
//Rewrite the argument of printData
printData(name, 27);
//Please do not rewrite this
printData("John Christopher Smith", 65);
}
public static void printData(String name, int age) {
System.out.println("my name is" + name + "is");
System.out.println("Age is" + age + "I'm old");
}
//Define fullName method
public static String fullName(String firstName ,String lastName){
return firstName + " " + lastName;
}
}
//Computers can determine as separate methods
hello(); //Hello method with no arguments
hello("Java"); //Method with one argument hello
Conditional branch by the return value (true / false value) of isHealthy method
Method 1
if (isHealthy(bmi)){
System.out.println("I'm healthy");
}else{
System.out.println("Not healthy");
}
Method 2
public static boolean isHealthy(double bmi){
return bmi >= 18.5 && bmi < 25.0;
}
Scanner You can also "enter" a value into the console and use that value in your program. A library called Scanner is used to receive input to the console.
//import required
import java.util.Scanner;
class Main {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("name:");
//Define the variable name, receive the string from the console and assign it
String name = scanner.next();
//Please be output as "Hello ◯◯-san"
System.out.println("Hello"+name+"Mr.");
}
}
Continue to Part 2 ....