2 回答

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊
我在為第二部分輸入時(shí)感到震驚,這將包括一個(gè) M 時(shí)間循環(huán),每次輸入 4 個(gè)輸入,分別為 IJKL
根據(jù)您的要求,我建議您創(chuàng)建一個(gè)類而不是使用原始類型。這比制作二維數(shù)組更容易,而且您可以對(duì)數(shù)據(jù)做更多的事情。
由于我不完全知道這個(gè)問(wèn)題,我會(huì)假設(shè)(建筑物的地板、建筑物的窗戶、爬上建筑物所花費(fèi)的時(shí)間、過(guò)馬路所花費(fèi)的時(shí)間)都是道路的屬性。如果沒(méi)有,那么您可以對(duì)建筑物進(jìn)行同樣的操作并將屬性從道路移動(dòng)到建筑物
道路類
public class Road {
private int floorsOfBuilding;
private int windowsInBuilding;
private int climbTime;
private int crossingTime;
// Constructor
public Road(int floorsOfBuilding, int windowsInBuilding, int climbTime, int crossingTime) {
this.floorsOfBuilding = floorsOfBuilding;
this.windowsInBuilding = windowsInBuilding;
this.climbTime = climbTime;
this.crossingTime = crossingTime;
// You can do simple calculations here but for more complex it is better
// to create a method to maintain readability
}
public String toString() {
return "\n" + floorsOfBuilding + " " + windowsInBuilding + " " + climbTime + " " + crossingTime;
}
}
主課
int roadsCount = Integer.parseInt(buildingsAndRoadsArray[1]);
Road[] roadsArray = new Road[roadsCount];
for (int i = 0; i < roadsArray.length; i++) {
System.out.println();
System.out.println("Road:" + i);
System.out.print("Enter I J K L: ");
String input = scan.nextLine();
String[] inputSplit = input.split(" ");
Road road = new Road(Integer.parseInt(inputSplit[0]), Integer.parseInt(inputSplit[1]),
Integer.parseInt(inputSplit[2]), Integer.parseInt(inputSplit[3]));
roadsArray[i] = road;
}
scan.close();
System.out.println();
for (int i = 0; i < roadsArray.length; i++) {
System.out.println("Road: " + i + roadsArray[i] + "\n");
}
輸出
Enter Number of Buildings and Roads: 1 2
// Input
Road:0
Enter I J K L: 12 13 14 15
Road:1
Enter I J K L: 49 59 69 79
// Output
Road: 0
12 13 14 15
Road: 1
49 59 69 79

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊
for (int i = 0; i < M; i++) {
int I = sc.nextInt();
int J = sc.nextInt();
int K = sc.nextInt():
int L = sc.nextInt();
// your code here
}
由于輸入是常數(shù),您可以直接輸入數(shù)字。
添加回答
舉報(bào)