Skip to content
domanthan edited this page Jun 22, 2020 · 1 revision
/**
 *
 */
package lesson04;

import java.util.Date;

/**
 * @author gridscale
 *
 */
public class Statement {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 値をセット。 assign
		//

		// 変数宣言と同時に値をセット。Declare,
		int i = 100, j = 200;

		// 式の評価値をセット。
		i = ++j * 100 + 29;
		System.out.println("i=" + String.format("%8d" ,i) );

		// new 式
		Date date = new Date();


		// if 文
		if (i > 100) System.out.println("i は100より大きい!");

		// 完全なif 文
		if (j % 100 == 0) {
			System.out.println("jは100の倍数!");
		} else if ( j % 100 == 1) {
			System.out.println("jは100を割って、1余り");
			if (i > 100) System.out.println("i は100より大きい!");
		} else {
			System.out.println("jは100を割って、" + (j % 100) + "余り");
		}

		// while : 条件を満たしている限り、繰り返す。
		//題: x = 20から始まり、1/x < 0.000 000 009になるまで、xを増やす処理を繰り返す。
		double x = 20.0;

		double EPSILON = 0.000000009;
		while (reciprocal(x) >= EPSILON) {
			x += 1;
		}
		System.out.println("X=" + String.format("%f",x));

		x = 20;
		while (x < 10000000000000.0) {
			if (reciprocal(x) < EPSILON) {
				break;
			}
			x += 1;
		}
		System.out.println("X=" + String.format("%f",x));

		int a[ ] = {12, 8, 10, 120, 113, 111, 126};

		i = 0;
		int number = 0;

		while (i < a.length) {
			if (a[i] % 3 == 0) {
				number++;
			} else {
				i++;
				continue;
			}
			System.out.print(a[i] + ",");
			if (number >= 2) {
				System.out.println("数足りました!");
				break;
			}
			i++;
		}

		System.out.println();
		System.out.println("Whie() : Number of times of 3 is " + number);

		//  何かを実行し、条件をチェック、条件を満たしている場合処理を最初の所に戻す。その繰り返しを行う。
		i = 0;

		do {
			if ( i>= a.length) {
				break;
			}

			if (a[i] % 3 == 0) {
				number++;
			} else {
				i++;
				continue;
			}
			System.out.print(a[i] + ",");
			if (number >= 2) {
				System.out.println("数足りました!");
				break;
			}
			i++;
		} while (i < a.length);

		System.out.println();
		System.out.println("do while(): Number of times of 3 is " + number);

		// try , catch
		int [] intNumbers = new int[] {12, 8, 10, 0, 120, 113, 111, 0, 126};
		for (int abc : intNumbers) {
			try {
			     System.out.println("Reciprocal of " + abc + " is:" + reciprocal(abc));
			} catch (ArithmeticException e) {
				System.out.println(e.getMessage());
			}
		}
	}

	/**
	 *
	 * @param x
	 * @return
	 */
	static double reciprocal(double x) {
		if ( x == 0) {
			throw new ArithmeticException("0を除数にしてはいけません");
		}
		return 1.0 / x;
	}

}