アットウィキロゴ
note4recurrent @ ウィキ
掲示板 掲示板 ページ検索 ページ検索 メニュー メニュー

note4recurrent @ ウィキ

JAVA ソース集 self study

最終更新:

note4recurrent

- view
だれでも歓迎! 編集

練習問題

	System.out.println("ようこそ占いの館へ");
	System.out.println("あなたの名前を入力してください");
	String name = new java.util.Scanner(System.in).nextLine();
	System.out.println("あなたの年齢を入力してください");
	String ageString =  new java.util.Scanner(System.in).nextLine();
	int age = Integer.parseInt(ageString);
	int fortune = new java.util.Random().nextInt(3);
	fortune ++;
	System.out.println("占いの結果が出ました");
	System.out.println(age+"歳の" + name + "さん、あなたの運気番号は" + fortune +"です");
	System.out.println("1:大吉、2:中吉、3:吉、4:凶");	

文字列の比較

String str = "文字列";
if (str.equals("文字列")) {
	System.out.println("success");
}

複数条件の書き方

  • && は and、|| は or と同じ
    		int age = 25;
    		boolean man = true;
    		if (age >= 20 && man == true) {
    		System.out.println("成年男子");
    		} else {
    		System.out.println("成年男子ではない");
    		}
    		age = 10;
    		if (age >= 20 && man == true) {
    			System.out.println("成年男子");
    		} else {
    			System.out.println("成年男子ではない");
    		}
    		if (age >= 20 || man == true) {
    			System.out.println("成年か男子");
    		}else {
    			System.out.println("女性で19歳以下");
    		}
    	//() と組み合わせることでさらに絞り込むことができる	
    		age = 10;
    		man = true;
    		if ((age >= 20 && man == true) ||(age >= 16 && man == false) ) {
    			System.out.println("20歳以上男性か16歳以上女性");
    		}
    

for ループ BASIC のfor next とほぼ同じ。 Pythonでは for i in range(10) みたいに書く

//基本形
		for (int i = 0 ;i<10; i++) { //配列は0から始まるので0からスタートすることが多い
			System.out.println(i);
		}
//覚えておきたい for ループの記述法
		int[] scores = new int[] {3,4,5,6};  //int[] scores = {3,4,5,6}; と書いてもいい
		for (int value : scores) {
			System.out.println(value);
		}

文字列の場合

		String[] scores = new String[] {"hoge","hoka","one","one"}; //String[] scores = {"hoge","hoka","one","one"};  も可
		for (String value : scores) {
			System.out.println(value);
		}  

変数のスコープ

  • ループ内やifブロックの中で宣言された変数はブロックの外では使えない。
  • ループの前に宣言した変数名をループで使おうとするとエラーになる。
  • ループの後で同じ名前を宣言してもエラーにはならない。
  • 結論>ループの制御変数をグローバル変数のように使わないほうが良い。
  • ブロックより前で宣言しておけばループを出た後も変数は残っている。ループの回数を使いたいような場合には前もって宣言しておくこと。
		int i;
		for (i = 1 ;i<10; i++) {
			if (i < 9) {
				System.out.print(i+",");
			} else {
				System.out.println(i);
			}
		}
		System.out.println(i);
実行結果:for内を9回周ってiに10が入ってループを抜けている 
1,2,3,4,5,6,7,8,9
10
  • 練習3-6
    		System.out.println("数当てゲーム");
    		int ans = new java.util.Random().nextInt(9);
    		for (int i = 1 ;i<5 ; i++) {
    			System.out.println("0〜9の数字を入力してください");
    			int num = new java.util.Scanner(System.in).nextInt();
    			if (ans == num) {
    				System.out.println("アタリ");
    				break;
    			} else {
    				System.out.println("違います");
    			}
    		}
    		System.out.println("ゲームを終了します");
    

配列のコピーP164

		int[] arrayA = {1,2,3,4};
		int[] arrayB;
		arrayB = arrayA;
		arrayB[0] = 100;
		System.out.println("A"+Arrays.toString(arrayA));
		System.out.println("B"+Arrays.toString(arrayB));
		
		int[] arrayC = {1,2,3,4,5};
		int[] arrayD = new int[arrayC.length];
		System.arraycopy(arrayC,0,arrayD,0,arrayC.length);
		arrayD[0] = 100;
		System.out.println("C"+Arrays.toString(arrayC));
		System.out.println("D"+Arrays.toString(arrayD)); 

           実行結果
A[100, 2, 3, 4]=B[0]に代入した数字がarrayA[0]に反映している
B[100, 2, 3, 4];配列の要素数は指定しなかったが arryaA を代入することで同じ要素数が作られている。
C[1, 2, 3, 4, 5] System.arracopyを使うと新たにDが作られるのでD[0]に代入した数字は影響していない
D[100, 2, 3, 4,5]
  • 配列を渡す
    	public static void printArray(int[] uketoriArray) {
    		for(int i = 0 ; i<3 ; i++) {
    			uketoriArray[i] = uketoriArray[i]+100;
    			System.out.println(uketoriArray[i]);
    			//サブルーチン内で値を一時的に操作したい場合は配列内で配列を作ってコピー
    			int[] kariHairetsu = uketoriArray;
    			System.out.println("kari" + uketoriArray[i]);
    		}
    	}
    
  • 配列を返す
    	// 配列を返す
     		public static int[] makeArray(int size) {
     			int[] newArray = new int[size];
     			for ( int i=0; i < newArray.length;i++) {
     				newArray[i]=i;
     			}
     			return newArray;
     		}
     		
     		public static void main(String[] args) {
     			int[] array = makeArray(3);
     			for (int i : array) {
     				System.out.println(i);
     			}
     		} 
    

練習問題5.8

	public static void introduceOneself () {
		String name = "匿名希望";
		int age = 60;
		double height = 165.5;
		char eto = '牛';
		System.out.println("私は"+name+"と申します Age"+age+"歳 身長は"+height+"cmで 干支は"+eto);
	}
	public static void main(String[] args) {
		introduceOneself();
	}
  • 5−2
    	public static void email(String title, String address, String text) {
    		System.out.println(address+"に、以下のメールを送信しました");
    		System.out.println("件名:"+title);
    		System.out.println("本文:"+text);
    	}
    
    	public static void main(String[] args) {
    		email("至急お支払ください","[email protected]","あなたが見たエロサイトの使用料は100万円です。");
    	}
    
  • 5-3
    	public static void email(String title, String address) {
    		System.out.println(address+"に、以下のメールを送信しました");
    		System.out.println("件名:無題");
    	}
    	public static void email(String text) {
    		System.out.println("本文:"+text);
    	}
    	public static void main(String[] args) {
    		email("","[email protected]");
    		email("あなたが見たエロサイトの使用料は100万円です。");
    	}
    
  • 5-4
    	public static double calcTriangleArea(double bottom, double height) {
    		double triangleArea = bottom * height /2;
    		return triangleArea;
    	}
    	public static double calcCircleArea(double radius) {
    		double cirleleArea = radius * radius * 3.14;
    		return cirleleArea;
    	}
    	
    	public static void main(String[] args) {
    		System.out.println("底辺10cm,高さ5cmの三角形の面積は"+calcTriangleArea(10,5)+"cm2");
    		System.out.println("半径15cmの円の面積は"+calcCircleArea(15)+"cm2");
    	}
    

object 8章

  • 練習問題
    • 以下のクラスファイルをHero,Matango,Mainと同じところに Cleric.java として作る。
public class Cleric {

	String name;
	int hp = 50;
	final int maxHp = 50;
	int mp = 10;
	final int maxMp = 10;

	public void selfAid() {
		System.out.println(this.name + "はセルフエイドを唱えた");
		this.mp -= 5;
		this.hp += maxHp;
		System.out.println(this.name + "のHPは最大まで回復した");
	}
	public int pray(int sec) {
		System.out.println(this.name + "は"+ sec + "秒間天に祈った");
		int recov = new java.util.Random().nextInt(3);
		recov = sec + recov; //秒とランダムで得た数値を回復とする
		if((this.maxMp-this.mp) < recov) { //回復値が最大を超える場合の例外
			recov = this.maxMp - this.mp;
		} 
		this.mp += recov;
		System.out.println("MPが"+recov+"回復した");
		return recov;
	}
	public void atackMagic() {
		System.out.println(this.name + "は攻撃呪文を唱えた");
		this.mp -= 3;
		System.out.println(this.name + "の残りMPは"+this.mp+"になった");
	}
}
  • Main
    public class Main {
    
    	public static void main(String[] args) {
    		// TODO 自動生成されたメソッド・スタブ
    		Hero h = new Hero();
    		h.name = "ミナト";
    		h.hp = 100;
    
    		Matango m1  = new Matango();
    		m1.hp = 50;
    		m1.suffix = 'A';
    		
    		Matango m2  = new Matango();
    		m2.hp = 48;
    		m2.suffix = 'B';
    
    		//聖職者
    		Cleric soryo  = new Cleric();
    		soryo.name = "ラスプーチン";
    		
    		//冒険の始まり
    		System.out.println("勇者"+ h.name + "は" +soryo.name +"と旅することになった。");
    		
    		h.slip();
    		soryo.atackMagic();
    		int byou = 1;
    		soryo.pray(byou);
    		System.out.println(soryo.name+"no mp ha "+ soryo.mp+"ni natta");
    		
    		m1.run();
    		m2.run();
    		h.run();
    	}
    }
    
  • 実行結果
    勇者ミナトはラスプーチンと旅することになった。
    ミナトは転んだ!
    5のダメージ
    ラスプーチンは攻撃呪文を唱えた
    ラスプーチンの残りMPは7になった
    ラスプーチンは1秒間天に祈った
    MPが3回復した
    ラスプーチンno mp ha 10ni natta
    おばけキノコAは逃げ出した!
    おばけキノコBは逃げ出した!
    ミナトは逃げ出した!
    GAMEOVER
    最終HPは95でした
    

9章

  • Hero class
    public class Hero {
    
    	String name;
    	int hp;
    	int level = 10;
    	Sword sword; //剣はいるよね
    	
    	public void sleep(){
    		this.hp = 100;
    		System.out.println(this.name + "is recovered ");
    	}
    
    	public void sit(int sec) {
    		this.hp += sec;
    		System.out.println();
    		System.out.println("HPが"+sec+"ポイント回復した");
    	}
     	
    	public void slip() {
    		this.hp -=5;
    		System.out.println(this.name + "は転んだ!");
    		System.out.println("5のダメージ");
    	}
    	 
    	public void run() {
    		System.out.println(this.name + "は逃げ出した!");
    		System.out.println("GAMEOVER");
    		System.out.println("最終HPは" + this.hp + "でした");
    	}
    	//コンストラクタ
    	public Hero(String name) {
    		this.hp = 100; //main で代入する必要がなくなる
    		this.name = name; //
    	}
    	
    }
    
  • マタンゴ
    public class Matango {
    
    	int hp;
    	int level = 10;
    	char suffix;
    	
    	public void run() {
    		System.out.println("おばけキノコ"+this.suffix + "は逃げ出した!");
    	}
    	//コンストラクタ
    	public Matango() {
    		this.hp = 50;
    	}
    }
    
  • Wizard
    public class Wizard {
    
    	String name;
    	int hp;
    	
    	public void heal(Hero h) {
    		h.hp += 10;
    		System.out.println(this.name +"は"+ h.name + "のHPを10回復した!");
    	}
    	//コンストラクタ
    	public Wizard(String name) {
    		this.hp = 100;
    		this.name = name;
    	}
    
    } 
    
  • Cleric : maxHp の初期化の方法が合っているかわからない。
    public class Cleric {
    
    	String name;
    	int hp;
    	final int maxHp = 50;
    	int mp;
    	final int maxMp = 10;
    
    	public void selfAid() {
    		System.out.println(this.name + "はセルフエイドを唱えた");
    		this.mp -= 5;
    		this.hp += maxHp;
    		System.out.println(this.name + "のHPは最大まで回復した");
    	}
    	public int pray(int sec) {
    		System.out.println(this.name + "は"+ sec + "秒間天に祈った");
    		int recov = new java.util.Random().nextInt(3);
    		recov = sec + recov; //秒とランダムで得た数値を回復とする
     		if((this.maxMp-this.mp) < recov) { //回復値が最大を超える場合の例外
     			recov = this.maxMp - this.mp;
    		} 
    		this.mp += recov;
    		System.out.println("MPが"+recov+"回復した");
    		return recov;
    	}
    	
    	public void atackMagic() {
    		System.out.println(this.name + "は攻撃呪文を唱えた");
    		this.mp -= 3;
    		System.out.println(this.name + "の残りMPは"+this.mp+"になった");
    	}
    	//コンストラクタで初期値セット
    	public Cleric(String name) {
    		this.hp = 50;
    		this.mp = 10;
    		this.name = name;
    	}
    	
    }
    
  • Sword
    public class Sword {
    
    	String name;
    	int damage;
    
    }
    
  • Main コンストラクタのおかげでメインがスッキリした
    public class Main {
    
    	public static void main(String[] args) {
    	
    		Sword s1 = new Sword(); //剣
     		s1.name = "剛力丸";//h1が持つ剣の名前
    		s1.damage = 10; //剣固有の攻撃力
    		Sword s2 = new Sword(); //剣
    		s2.name = "ロトの剣";//h1が持つ剣の名前
    		s2.damage = 12; //剣固有の攻撃力
    		
    		Hero h1 = new Hero("シオン");
    		h1.sword = s1; //上で作ったs1をh1と紐付ける
    		
    		Hero h2 = new Hero("アサカ");
    		h2.sword = s2;
    		
    		Wizard w = new Wizard("スガワラ"); 
    
    		Cleric soryo  = new Cleric("ラスプーチン");
    		
    		Matango m1  = new Matango();
    		m1.suffix = 'A';
    		
    		Matango m2  = new Matango();
    		m2.suffix = 'B';
    
    		
    		//冒険の始まり
    		System.out.println("勇者"+ h1.name + "は" +h1.sword.name +"を装備した。");
    		System.out.println("勇者"+ h2.name + "は" +h2.sword.name +"を装備した。");
    		System.out.println("勇者"+ h1.name + "は" +soryo.name +"と旅することになった。");
    		System.out.println("勇者"+ h1.name + "は" + h2.name +"と出会って意気投合。");
    		
    		h1.slip();
    		w.heal(h1);//h1を回復
    		
    		soryo.atackMagic(); //僧侶の魔法攻撃
    		
    		int byou = 1; //回復に唱える時間(秒)
    		soryo.pray(byou);
    		System.out.println(soryo.name+"no mp ha "+ soryo.mp+"ni natta");
    		
    		m1.run();
    		m2.run();
    		h1.run();
    	}
    }
    
  • 実行結果
    勇者シオンは剛力丸を装備した。
    勇者アサカはロトの剣を装備した。
    勇者シオンはラスプーチンと旅することになった。
    勇者シオンはアサカと出会って意気投合。
    シオンは転んだ!
    5のダメージ
    スガワラはシオンのHPを10回復した!
    ラスプーチンは攻撃呪文を唱えた
    ラスプーチンの残りMPは7になった
    ラスプーチンは1秒間天に祈った
    MPが2回復した
    ラスプーチンno mp ha 9ni natta
    おばけキノコAは逃げ出した!
    おばけキノコBは逃げ出した!
    シオンは逃げ出した!
    GAMEOVER
    最終HPは105でした
    

タグ:

JAVA
+ タグ編集
  • タグ:
  • JAVA
最近更新されたスレッド
ウィキ募集バナー