2 回答
TA贡献1825条经验 获得超6个赞
我在为第二部分输入时感到震惊,这将包括一个 M 时间循环,每次输入 4 个输入,分别为 IJKL
根据您的要求,我建议您创建一个类而不是使用原始类型。这比制作二维数组更容易,而且您可以对数据做更多的事情。
由于我不完全知道这个问题,我会假设(建筑物的地板、建筑物的窗户、爬上建筑物所花费的时间、过马路所花费的时间)都是道路的属性。如果没有,那么您可以对建筑物进行同样的操作并将属性从道路移动到建筑物
道路类
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贡献1946条经验 获得超3个赞
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
}
由于输入是常数,您可以直接输入数字。
添加回答
举报