The first line contain two separated integer N and M. Where N stands for Number of Buildings and M stands for Roads.
Next follows M line, each lines consists of 4 integers IJKL representing Floor of Building, Windows in building,Time taken to climb the building, Time taken to cross road.
I have been able to taken the first input as follows:
Scanner sc = new Scanner(System.in);
System.out.println("Enter N and M");
String NandM = sc.nextLine();
String [] NandMAsArray = NandM.split(" ");
int N = Integer.parseInt(NandMAsArray[0]);
int M = Integer.parseInt(NandMAsArray[1]);
I am struck while taking input for second part , which will consist of a loop of M time and taking 4 inputs each time, IJKL
respectively.
How do i do this??
input and output for second part should be like this:
Input:
Road:0
12 34 44 56
Road:1
22 76 89 90
https://i.postimg.cc/28fQ3dVR/Screenshot-20190807-121551-Chrome.jpg
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
}
Since the input is constant, you can just take in the numbers directly.
I am struck while taking input for second part , which will consist of a loop of M time and taking 4 inputs each time, IJKL respectively
Based on your requirement I'd suggest you create a class rather than using primitive types. This will be easier than making a 2D array and you can do more with data.
Since I don't exactly know the problem I am gonna assume that (Floor of Building, Windows in building,Time taken to climb the building, Time taken to cross road) all are properties of road. If not then you can same thing to buildings and move the attributes from road to building
Road Class
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;
}
}
Main Class
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");
}
Output
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.