有關陣列陣列 (Array)的問題java

我對於這個題目不知道該如何下手,我使用split但是我轉不過去int型態...

String all[]=input.split(" "); int[] data=new int[all.length]; for(int i=0;i 我不知道是哪裡出了問題,請告訴我我的問題在哪裡,或者請告訴我應該要換哪一種方法會比較好?

問題描述 : 某遊覽車派遣公司共收到n筆任務訂單,訂單中詳細記載發車時間s和返回時間d。每一輛遊覽車只要任務時間不衝突,可立即更換司機繼續上路執行任務。請問該公司至少需要調遣多少車輛才足以應付需求?輸入說明 : 程式的輸入包含兩行數字,第一行包含一個正整數n,1 ≤ n ≤ 30,代表第二行有n筆訂單的出發時間和返回時間s1, d1, s2, d2, ..., sn, dn,0 < si < di ≤ 24,而此2n個正整數間以空格隔開。Sample Input: Sample Output: 3

1 6 3 12 6 18 2

Update:

String all[]=input.split(" ");

int[] data=new int[all.length];

for(int i=0;i

Update 2:

部分內容被截掉了...

all.length;i++){

int temp=Integer.parseInt(all[i]);

data[i]+=temp;

}

System.out.println(data[1]);

Update 3:

請問

Integer.parseInt

String.parseInt

不同的地方

Update 4:

Will Integer.parseInt different place with String.parseInt

1 Answer

Rating
  • John
    Lv 7
    6 years ago
    Favorite Answer

    import java.util.ArrayList;

    import java.util.List;

    import java.util.Scanner;

    public class Program {

    public static void main(String[] args) {

    Program app = new Program();

    app.one(); // your question

    app.two(); // the solution

    }

    public void one() {

    String input = "1 6 3 12 6 18";

    String[] inputs = input.split("\\D");

    int[] data = new int[inputs.length];

    for (int i = 0; i < inputs.length; i++) {

    data[i] = Integer.parseInt(inputs[i]);

    System.out.printf("%d%n", data[i]);

    }

    }

    public void two() {

    String input = read("Enter list of departure and arrival time? ");

    // test data: "1 6 3 12 6 18 11 12 18 19 20 21";

    String[] inputs = input.split("\\D");

    List<Bus> buses = new ArrayList<Bus>(0);

    for (int i = 0; i < inputs.length; i += 2) {

    int departureAt = Integer.parseInt(inputs[i]); // arrival at

    int arrivalAt = Integer.parseInt(inputs[i + 1]); // departure at

    boolean matched = false;

    for (Bus bus : buses) {

    matched = bus.isAvailable(departureAt);

    if (matched) {

    bus.setArrivalAt(arrivalAt);

    bus.setDepartureAt(departureAt);

    break;

    }

    }

    if (!matched) {

    buses.add(new Bus(departureAt, arrivalAt));

    }

    }

    System.out.printf("%d number of bus is needed%n", buses.size());

    }

    private String read(String message) {

    String result = "";

    System.out.print(message);

    Scanner keyboard = new Scanner(System.in);

    result = keyboard.nextLine();

    return result;

    }

    }

    class Bus {

    private int departureAt;

    private int arrivalAt;

    public Bus(int departureAt, int arrivalAt) {

    setArrivalAt(arrivalAt);

    setDepartureAt(departureAt);

    }

    public boolean isAvailable(int departureAt) {

    return (departureAt >= this.arrivalAt);

    }

    public void setDepartureAt(int departureAt) {

    this.departureAt = departureAt;

    }

    public void setArrivalAt(int arrivalAt) {

    this.arrivalAt = arrivalAt;

    }

    }

Still have questions? Get your answers by asking now.