Created
December 1, 2016 05:29
-
-
Save kdmgs110/4a6fd095f18973d818c7d2ed5fa23ba3 to your computer and use it in GitHub Desktop.
メソッド(returnとかもろもろ)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Inputメソッドを使って、挨拶プログラムを作成する | |
*/ | |
package pass01; | |
import lib.Input; | |
public class Pass01_01 { // クラスの名前を宣言 {はブロック | |
public static void main(String[] args) { | |
System.out.println("Start"); | |
display(); // メソッド | |
disBmi(1.65,58.0); // bmiメソッド。下のdisBmiを呼び出す。 | |
getBmi(1.75,60.0); | |
System.out.println("End"); | |
/*<メソッドとは> | |
* クラスメソッドとインスタンスメソッドがある | |
*/ | |
double data = Math.sqrt(10); //クラスメソッド | |
int len = "Hello".length(); //インスタンスメソッド | |
}// ここまでがmainメソッド | |
public static void display(){ | |
System.out.println("こんにちは"); //ここにメソッドの呼び出しをしている | |
return; //強制的に呼び出し元に戻る | |
} | |
/*bmiを求めるメソッド | |
* | |
*/ | |
public static void disBmi(double height, double weight){ //引数にはheight、weightを入れる | |
double bmi = weight / Math.pow(height, 2); | |
System.out.println("BMIは"+bmi + "です。"); | |
return; //強制的に呼び出し元に戻る | |
} | |
public static double getBmi (double height, double weight){ //return値があるからvoidではない。double戻り値型 | |
double bmi = weight / Math.pow(height, 2); //bmiの値をしたに送る | |
return bmi; //bmiという値をgetBmiメソッドに返す。データがdouble型 | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment