落書きノート

ふと自分が気になった事を書いてます

Javaで問題を解く

解きながら学ぶJava 入門編

解きながら学ぶJava 入門編

// q7_4
import java.util.Scanner;

class q7_4 {
    static int sumUp(int n) {
        int sum = 0;
        for(int i = 1; i <= n; i++)
            sum += i;
        return sum;
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("1からxまでの和を求めます。");
        do {
            System.out.print("xの値 : ");
            int x = scan.nextInt();
        } while(x <= 0);
        System.out.println("1から" + x + "までの和は" + sumUp(x) + "です。");
    }
}

// q7_5
import java.util.Scanner;

class q7_5 {
    static void hello() {
        System.out.println("こんにちは。");
    }
    public static void main(String[] args) {
        hello();
        hello();
        hello();
    }
}

// q7_6
import java.util.Scanner;

class q7_6 {
    static void printSeason(int m) {
        switch(m) {
        case 12:
        case 1:
        case 2:
            System.out.println("冬");
            break;
        case 3:
        case 4:
        case 5:
            System.out.println("春");
            break;
        case 6:
        case 7:
        case 8:
            System.out.println("夏");
            break;
        case 9:
        case 10:
        case 11:
            System.out.println("秋");
            break;
        }
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("何月ですか(1~12) : ");
        printSeason(scan.nextInt());
    }
}

// q7_7
import java.util.Scanner;

class q7_7 {
    static void putChars(char c, int n) {
        for(int i = 0; i < n; i++)
            System.out.print(c);
    }
    static void putStars(int n) {
        putChars('*', n);
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("左下直角の三角形を表示します。");
        System.out.print("段数は : ");
        int n = scan.nextInt();
        for(int i = 1; i <= n; i++) {
            putStars(i);
            System.out.println();
        }
    }
}

// q7_8
import java.util.Scanner;
import java.util.Random;

class q7_8 {
    static int random(int a, int b) {
        if(a > b)
            return a;
        Random rand = new Random();
        return rand.nextInt(b - a) + a;
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("下限値 : ");
        int a = scan.nextInt();
        System.out.print("上限値 : ");
        int b = scan.nextInt();
        System.out.println("生成した乱数は" + random(a, b) + "です。");
    }
}

// q7_9
import java.util.Scanner;

class q7_9 {
    static int readPlusInt() {
        int num;
        Scanner scan = new Scanner(System.in);
        do {
            System.out.print("正の整数値 : ");
            num = scan.nextInt();
        } while(num <= 0);
        return num;
    }
    public static void main(String[] args) {
        int source,num,answer = 0;
        int flag;
        Scanner scan = new Scanner(System.in);
        do {
            num = readPlusInt();
            source = num;
            while(num != 0) {
                answer = answer * 10 + num % 10;
                num /= 10;
            }
            System.out.println("逆から読むと" + answer + "です。");
            System.out.print("もう一度?<Yes...1/No...0> : ");
            flag = scan.nextInt();
            answer = 0;
        } while(flag == 1);
    }
}