0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

paizaラーニング Bランクレベルアップメニュー 足すか掛けるか Java 解答

Posted at

STEP: 7 インクリメント

問題

解答

step7.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int total = addStandardInputAndM(n, 1);
        System.out.println(total);
    }
    
    /**
     *  標準入力とmの足し算をする
     * 
     * @param n 標準入力
     * @param m 足したい数字
     * 
     *  @return 計算結果
     */
    public static int addStandardInputAndM(int n, int m){
        int total = 0;
        total =  n + m;
        return total;
    }
}

結果

image.png

STEP: 8 文字列の出力

問題

解答

step8.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.nextLine();
        for(int i = 0; i < Integer.valueOf(n); i++){
            String line = sc.nextLine();
            System.out.println(line);
        }
        sc.close();
    }
}

結果

image.png

STEP: 9 文字列の分割

問題

解答

step9.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.next();
            System.out.println(str);
        }
    }
}

結果

image.png

STEP: 10 整数の足し算

問題

解答

step10.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
   
        Scanner sc = new Scanner(System.in);
        int total = 0;
        while(sc.hasNext()){
            total += Integer.valueOf(sc.next());
        }
        System.out.println(total);
    }
}

結果

image.png

FINAL問題 足すか掛けるか

問題

解答

final.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        // 自分の得意な言語で
        // Let's チャレンジ!!
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int total = 0;
        for(int i = 0; i < n; i++){
            int a = Integer.valueOf(sc.next());
            int b = Integer.valueOf(sc.next());
            total += addOrMultiply(a, b);
        }
        System.out.println(total);
    }
    
    public static int addOrMultiply(int a, int b){
        int total = 0;
        if(a == b){
            total = a * b;
        } else {
            total = a + b;    
        }
        return total;
        
    }
}

結果

image.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?