简体   繁体   English

如何接受自定义输入

[英]How to Take custom inputs

The first line contain two separated integer N and M. Where N stands for Number of Buildings and M stands for Roads. 第一行包含两个分开的整数N和M。其中N代表建筑物数量,M代表道路。

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. 接下来的M行,每行由4个整数IJKL组成,分别代表建筑物的楼层,建筑物中的Windows,爬建筑物所需的时间,过马路所需的时间。

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. 我在输入第二部分时感到震惊,这将包括一个M时间循环,每次输入4次输入,分别是IJKL

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 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 我在输入第二部分时感到震惊,这将包括M次循环并每次输入4次输入,分别为IJKL

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. 这将比制作2D数组容易,而且您可以处理更多数据。

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. 由于我不完全了解问题,因此我将假定(建筑物地板,建筑物中的Windows,爬建筑物所需的时间,过马路所需的时间)都是道路的属性。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM