落書きノート

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

Javaで問題を解く

import java.util.Scanner;

class q4_15 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("nの値 : ");
        int n = scan.nextInt();
        int answer = 0;
        String s = "";
        for(int i = 1; i <= n; i++) {
            answer += i;
            s += String.valueOf(i);
            if(i != n)
                s += " + ";
        }
        s += " = " + String.valueOf(answer);
        System.out.println(s);
    }
}



import java.util.Scanner;

class q4_15_another {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("nの値 : ");
        int num = scan.nextInt();
        int sum = 0;
        for(int i = 1; i < num; i++) {
            System.out.print(i + " + ");
            sum += i;
        }
        System.out.print(num + " = ");
        sum += num;

        System.out.println(sum);
    }
}




import java.util.Scanner;

class q4_16 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("何センチから : ");
        int from_num = scan.nextInt();
        System.out.print("何センチまで : ");
        int to_num = scan.nextInt();
        System.out.print("何センチごと : ");
        int step = scan.nextInt();
        System.out.println("身長 標準体重");
        System.out.println("------------");
        for(int i = from_num; i <= to_num; i += step) {
            System.out.println(i + "  " + ((i - 100) * 0.9));
        }
    }
}




import java.util.Scanner;

class q4_17 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("何個*を表示しますか : ");
        int asterisk = scan.nextInt();
        for(int i = 1; i <= asterisk; i++) {
            System.out.print("*");
            if(i % 5 == 0)
                System.out.println();
        }
        if(asterisk % 5 != 0)
            System.out.println();
    }
}




import java.util.Scanner;

class q4_18 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("整数値 : ");
        int n = scan.nextInt();
        for(int i = 1; i <= n; i++) {
            if(n % i == 0)
                System.out.print(i + " ");
        }
        System.out.println();
    }
}




import java.util.Scanner;

class q4_19 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("nの値 : ");
        int n = scan.nextInt();
        for(int i = 1; i <= n; i++){
            System.out.println(i + "の2乗は" + (int)Math.pow((double)i, (double)i));
        }
    }
}