Java
It is basically defined in the class. It's like a part that summarizes the processing. [Example]
public static void hello();{
System.out.println("Hello World");
}
Remember once as a fixed phrase up to public ... void.
Additional information given to the method.
In () after hello earlier You can pass arguments such as "(String name)".
When passing multiple arguments, write "(String name, int price)" ... The first is called the first argument and the second is called the second argument.
It is used when the caller of the method wants to use the result. By the way, the void of "public static void .." that I used so far is Remember, it means no return value. [Example]
....
public static void main(String[]args){
String name=fullName("Yamada","Taro") /*← Be careful not to forget the full Name here!*/
printData(name,27);
}
public static void printData(String name,int age){
System.out.println("my name is"+name);
System.out.println("My age is"+age);
}
public static String fullName(String firstName,String lastName){
return firstName+lastName;
}
Until now, I made many methods in parallel with the main method Use classes because there are too many parallels and it's hard to see. Therefore, it is necessary to call the methods of other classes. [Example]
/*Main class*/
class Main{
public static void main(String[]args){
Person.hello();
}
}
/*Person class*/
class Person{
public static void hello(){
System.out.println("Hello");
}
}
Main class → execution class Person class → logic (logic class) It is easy to clarify the role when separated from.
It is possible to use classes created by other people using import. For example, a major example is a class called Math that has mathematical methods. To load it, in class ↑ You need to write "import java.lang.Math;".
[Example] How to write when you want to return the larger of the two arguments
import java.lang.Math;
class Main{
public static void main(String[]args){
int max=Math.max(3,8);
System.out.println("The maximum value is"+max+"is.");
}
}
[Example] How to write when rounding off after the decimal point of the argument
/*...Omitted...Output part only*/
("BMI"+Math.round(bmi)+"is.");
Scanner It is possible to receive the character string entered in the console.
import java.util.Scanner;
class Main{
public static void main (String[]args){
Scanner scanner=new Scanner(System.in);
/*↑ Scanner initialization*/
System.out.print("name:");
/*At this point, execution is temporarily stopped and it becomes a state of waiting for input.*/
String name=scanner.next();
/*The scanner here is lowercase! ".Don't forget! Call the character string entered in ↑*/
System.out.println("Name is"+name+"is");
other An integer with scanner.nextInt () ;. You can receive decimals with scanner.nextDouble () ;.
When dividing methods and writing the return value by yourself If you don't write them in order, they will get messed up. You have to understand each one and proceed. There are many parts that I'm not used to yet, so I switched to Progate's dojo mode. I will take some time to challenge myself.
Recommended Posts