method.jave
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
It is troublesome to write. If you use the method, you can write as follows.
method.jave
void sayHelloWorld(int n){
for(int i=1; i<=n; i++){
System.out.println("Hello World");
}
}
sayHelloWorld(10)
Once you have created a method for that process, you can call it at any time.
Of course, even if the type name is "void", it is possible to get the result calculated by "System.out.println ();". The return value is effective when you want to obtain the result of processing as "moving decimal point, integer value, boolean value" instead of "character string".
For example Suppose you want to get the automatically calculated result by entering the list price of a certain product and the discount rate (% off) of that product.
method2.jv
double calculationOfDiscountPrice(int price, double discountRate){
double discountPrice = price - (price*discountRate/100);
return discountPrice
}
calculationOfDiscountPrice(1000,10)
↓ 900
You can get the result like this.
Recommended Posts