为了账号安全,请及时绑定邮箱和手机立即绑定

如何获取自定义输入

如何获取自定义输入

呼啦一阵风 2023-06-04 11:11:12
第一行包含两个分隔的整数 N 和 M。其中 N 代表建筑物数量,M 代表道路。接下来是 M 行,每行由 4 个整数 IJKL 组成,分别代表建筑物的楼层、建筑物的窗户、爬楼的时间、过马路的时间。我已经能够按如下方式接受第一个输入: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]);我在为第二部分输入时感到震惊,它由一个 M 时间循环组成,每次分别输入4 个输入。I J K L我该怎么做呢??第二部分的输入和输出应该是这样的:输入:Road:012 34 44 56Road:122 76 89 90https://i.postimg.cc/28fQ3dVR/Screenshot-20190807-121551-Chrome.jpg
查看完整描述

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


查看完整回答
反对 回复 2023-06-04
?
智慧大石

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

}

由于输入是常数,您可以直接输入数字。


查看完整回答
反对 回复 2023-06-04
  • 2 回答
  • 0 关注
  • 110 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信