0
0

More than 1 year has passed since last update.

paizaラーニング Bランクレベルアップメニュー 文字列を切り取る Java 解答

Posted at

STEP: 12 文字列の分割

問題

解答

step12.java
import java.util.*;


public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            System.out.println(sc.next());
        }
    }
}

結果

image.png

STEP: 13 整数の足し算

問題

解答

step13.java
import java.util.*;


public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println(addUp( sc.nextInt(), sc.nextInt()));
    }
    
    /**
     * 標準入力で得た引数の足し算をする関数
     * 
     * @param a 足し算したい数字
     * @param b 足し算したい数字
     * 
     * @return 合計
     */
    public static int addUp(int a, int b){
        return a + b;
    }
}

結果

image.png

STEP: 14 文字列の長さ

問題

解答

step14.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        System.out.println(getCharacterLength(s));
    }
    
    /**
     * 文字の長さを取得する関数
     * 
     * @param s 文字の長さを取得したい文字列
     * 
     * @return  文字の長さ
     */
    public static int getCharacterLength(String s){
        int lenght = s.length();
        
        return lenght;
    }
}

結果

image.png

STEP: 15 文字列の1文字目

問題

解答

step15.java
import java.util.*;


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

結果

image.png

STEP: 16 あいだの整数

問題

解答

step16.java
puts 'The best way to log and share programmers knowledge.'

結果

image.png

FINAL問題 文字列を切り取る

問題

解答

final.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
 
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b =  sc.nextInt();
       
        // 1行改行
        sc.nextLine();
        String s = sc.nextLine();
        System.out.println(cutOutLetters(a, b, s));
        sc.close();
    }
    
    /**
     * a 文字目から b 文字目の範囲で文字列を切り取る関数
     * 
     * @param a スタートの文字数
     * @param b エンドの文字数
     * 
     * @return 切り取った文字
     */
    public static String cutOutLetters(int a, int b, String s) {
        String clippingCharacter = null;
        // インデックスは0から始まる為、-1からスタート
        clippingCharacter = s.substring(a-1, b);
        
        return clippingCharacter;
    }
}

結果

image.png

0
0
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
0