Created
November 30, 2016 09:45
-
-
Save kdmgs110/db67b4d0fd0f5f8d08d6ecdb946a1e40 to your computer and use it in GitHub Desktop.
Javaの練習
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; //Input.javaをインポートする。インポート文。ほかのクラスを利用したいときは、パッケージ名+クラス名を記入する | |
public class Pass01_01 { // クラスの名前を宣言 {はブロック | |
public static void main(String[] args) { // メソッドを示す.これは特別なmainメソッド | |
String ss = "abAB日本語abAB"; | |
int len = ss.length();//文字列の長さを調べる。配列の数も数えられる | |
int pos = ss.indexOf("日");//その数字がどこにあるか示す | |
int ch =ss.charAt(4);//その値が何番目にあるか示す。012345 | |
String str = ss.toLowerCase();//小文字にする方法 | |
System.out.println("長さ" + len); | |
System.out.println("日は" + pos + "番目"); | |
System.out.println("4番目の文字は" + ch); | |
System.out.println(str); | |
int[] n = {5,12,13,14};//配列の入力方法。 | |
String[] s = {"こんにちは","さようなら","こんばんわ"}; | |
System.out.println(n[0]); //5 この場合1番にあるから。 | |
for (int i = 0; i<3 ; i++ ){ // for文 初期値、条件、どれだけ増えるかのインクリメント | |
System.out.println("やっほう"); | |
} | |
int[] num = {10,20,30,40,50}; | |
int total = 0; | |
for(int i=0; i<num.length;i++){ //numの配列の長さを限界値にすることで未知の値にも対応できる | |
System.out.println("今の数字は" + num[i] +"です"); | |
total += num[i]; // 10,20,30と足していく | |
System.out.println("今の合計値は" + total +"です"); | |
} | |
for(int i=0;i<num.length;i++) //命令文が一つなら{}いらない | |
System.out.println(num[i]); | |
for(int i=0;i<2;i++){ | |
for(int j=0;j<5;j++){ //ネストしたfor文。基本使わない。 | |
System.out.print(j); //12345を二回出力 | |
} | |
System.out.println(); | |
} | |
/* | |
* 拡張for文の作り方。初期値の設定はいらない。 | |
*/ | |
int[] number = {12,23,45,66}; | |
for(int numb:number){ //拡張for文 numbという変数にnumberの配列ぶち込んでいる | |
System.out.println(numb); | |
} | |
/* | |
* 文字列の比較(p178) | |
* if username == dai の代わりに、インスタンスメソッドで聞く | |
* username.equals("dai") | |
*/ | |
String username = Input.getString("Please put your name");//コンソールで「あなたの名前を教えてください」 | |
System.out.println(username.equals("dai"));//"Dai"と等しければtrue | |
/*条件演算子 | |
* | |
*/ | |
int count = Input.getInt(); //整数を取得してくる | |
int b = count % 2==0 ? 100:0;//取得した数が二で割れれば100、割れなければ0。条件演算子 | |
System.out.println(b);//bを出力 入力値が偶数であれば、200と出てくる | |
/* | |
* while文 | |
*/ | |
int abc = 0; | |
while(abc<3){ //3未満なら繰り替えす | |
System.out.println("うほ"); | |
abc++; | |
} | |
/* | |
* while文で、0を入力したら終わる方法 | |
*/ | |
int abc; | |
while((abc=Input.getInt("数字を入力してください。")) != 0){ // 数値を入力する 0以外なら次の処理へ | |
System.out.println(abc + "を入力しました。"); //入力数字が出てくる | |
} | |
/* | |
* P196 電卓プログラム。数字を足していき、最終的に出てきた合計値を出力する。 | |
*/ | |
int val,sumnumber = 0; //宣言を行う | |
while((val=Input.getInt("電卓で計算する数字を入力してください。")) !=0){ //出力した数字が0にならない間に | |
sumnumber+= val; //入力したvalをsumnumberにタス。これを繰り替えす | |
} | |
System.out.println("合計値は " + sumnumber + "です"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment