jmu-Java-02基本語法-02-StringBuilder

2020-10-05 11:01:08

輸入3個整數n、begin、end首先,使用如下程式碼:

for(int i=0;i<n;i++)

將從0到n-1的數位拼接為字串str。如,n=12,則拼接出來的字串為01234567891011

最後擷取字串str從beginend(包括begin,但不包括end)之間的字串,並輸出。

輸入樣例:

10
5
8
1000
800
900

輸出樣例:

567
0330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533


答案

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int n, begin, end;
            n = scanner.nextInt();
            begin = scanner.nextInt();
            end = scanner.nextInt();
            StringBuilder str = new StringBuilder("");
            for (int i = 0; i < n; i++) {
                str.append(i);
            }
            System.out.println(str.substring(begin,end));
        }

    }
}