简体   繁体   English

如何从java中的一行读取多个输入?

[英]How read multiple inputs from one line in java?

So i have this code :所以我有这个代码:

for(int i=0;i<number;i++) { 
System.out.println("WHAT HOUSEHOLD DO YOU WANT TO CONNECT IN APARTMENT NO."+(i+1));
        System.out.println("1 ) OVEN");
        System.out.println("2 ) TV");
        System.out.println("3 ) VACCUUM CLEANER");
        System.out.println("4 ) REFRIGERATOR");
        int option = cin.nextInt();
        switch(option) {
        case 1: {
            for(initiereAparate e : aparate)
            fw.write((i+1)+" "+e.ob1.denumire+" "+e.ob1.material+" "+e.ob1.culoare+" "+e.ob1.origine+" "+e.ob1.consumEnergie+"\r\n"); break;
        }
        case 2: {
            for(initiereAparate e : aparate)
            fw.write((i+1)+" "+e.ob2.denumire+" "+e.ob2.material+" "+e.ob2.culoare+" "+e.ob2.origine+" "+e.ob2.consumEnergie+"\r\n"); break;
        }
        case 3: {
            for(initiereAparate e : aparate)
            fw.write((i+1)+" "+e.ob3.denumire+" "+e.ob3.material+" "+e.ob3.culoare+" "+e.ob3.origine+" "+e.ob3.consumEnergie+"\r\n"); break;
        }
        case 4: {
            for(initiereAparate e : aparate)
            fw.write((i+1)+" "+e.ob4.denumire+" "+e.ob4.material+" "+e.ob4.culoare+" "+e.ob4.origine+" "+e.ob4.consumEnergie+"\r\n"); break;
        }
        }
}

I would like to know how to read multiple inputs.For example i will type in one line 1 2 4 , and this numbers will execute 3 cases at once.In my code that i already have i can type only 1 number and it will execute only one case statement我想知道如何读取多个输入。例如,我将输入一行 1 2 4 ,这个数字将一次执行 3 个案例。在我已经拥有的代码中,我只能输入 1 个数字,它将执行只有一个case语句

You would need to change your input function to expect a string rather than an int.您需要将输入函数更改为期望字符串而不是 int。 And then simply split your string into number and loop trought the number to execute your switch case.然后简单地将您的字符串拆分为数字并循环使用该数字来执行您的 switch case。

PS please note that this code is untested, don't just copy and paste it expecting everything to work perfectly. PS请注意,此代码未经测试,不要只是复制和粘贴它,期望一切都能正常工作。 Try to understand what I did and apply it to your own context.尝试理解我所做的并将其应用到您自己的上下文中。

for (int i = 0; i < number; i++) {
        System.out.println("WHAT HOUSEHOLD DO YOU WANT TO CONNECT IN APARTMENT NO." + (i + 1));
        System.out.println("1 ) OVEN");
        System.out.println("2 ) TV");
        System.out.println("3 ) VACCUUM CLEANER");
        System.out.println("4 ) REFRIGERATOR");
        // we change this to nextLine() because we want a string.
        String option = cin.nextLine();
        // we split our string at white space, this give us an array of string;
        String[] options = option.split("\\s+");
        // we parse every options in the array
        for (int j = 0; j < options.length; j++) {
            // we need to cast the option back to an integer since, it's what we are
            // comparing.
            int currentOption = Integer.parseInt(options[j]);
            switch (currentOption) {
            case 1: {
                for (initiereAparate e : aparate)
                    fw.write((i + 1) + " " + e.ob1.denumire + " " + e.ob1.material + " " + e.ob1.culoare + " "
                            + e.ob1.origine + " " + e.ob1.consumEnergie + "\r\n");
                break;
            }
            case 2: {
                for (initiereAparate e : aparate)
                    fw.write((i + 1) + " " + e.ob2.denumire + " " + e.ob2.material + " " + e.ob2.culoare + " "
                            + e.ob2.origine + " " + e.ob2.consumEnergie + "\r\n");
                break;
            }
            case 3: {
                for (initiereAparate e : aparate)
                    fw.write((i + 1) + " " + e.ob3.denumire + " " + e.ob3.material + " " + e.ob3.culoare + " "
                            + e.ob3.origine + " " + e.ob3.consumEnergie + "\r\n");
                break;
            }
            case 4: {
                for (initiereAparate e : aparate)
                    fw.write((i + 1) + " " + e.ob4.denumire + " " + e.ob4.material + " " + e.ob4.culoare + " "
                            + e.ob4.origine + " " + e.ob4.consumEnergie + "\r\n");
                break;
            }
        }
    }
}

As Arvind Kumar Avinash pointed out in the comment, it is important to add the keyword break to you switch cases, this ensure that you only treat one case per iteration.正如Arvind Kumar Avinash在评论中指出的那样,在 switch case 中添加关键字break很重要,这确保每次迭代只处理一个 case。

Yes, you can do that.是的,你可以这样做。 You can use following idea:您可以使用以下想法:

String option = cin.nextLine();
String[] data=option.split(" ") // split by space character
for(int i=0;i<data.length;i++){
int option = Integer.parseInt(data[i]);
switch(option)...}

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

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