如何在Java中循环用户输入

时间:2013-10-10 14:53:29

标签: java

到目前为止,我的代码看起来像这样:

    package kittensworld;

    import java.util.Scanner;

    public class KittensWorld {
        public static void main(String[] args){

            System.out.println("What is the first kitten's name?");
            Scanner scan = new Scanner(System.in);
            String kitten1 = scan.nextLine();
            System.out.println("What is the second kitten's name?");
            String kitten2 = scan.nextLine();
            System.out.println("And the third kiten's name?");
            String kitten3 = scan.nextLine();
            System.out.println("The first kitten's name is " + kitten1);
            System.out.println("The second kitten is " + kitten2);
            System.out.println("And last but not least, " + kitten3);
        }
    }

我如何循环播放它以便我可以询问20只小猫的名字,而无需复制和粘贴20次?

5 个答案:

答案 0 :(得分:2)

使用包含数据的for循环和array

String[] inputs = new String[NUMBER_OF_INPUTS];
Scanner scanner = new Scanner();
for(i=0; i<NUMBER_OF_INPUTS; i++) {
   inputs[i] = scanner.nextLine();
}

如果你不知道输入的数量(但你知道什么符号代表输入的结尾),你可以使用while循环和ArrayList

答案 1 :(得分:2)

我认为你需要查阅本教程,了解这个名为for-loop的正确循环:

String[] kitten = new String [size];
for(int i = 0 ; i < kitten.length; i++){
 System.out.println("kitten number "+i+" : ");
 kitten [i] = scan.nextLine();
}

答案 2 :(得分:1)

package kittensworld;

import java.util.Scanner;

public class KittensWorld {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        for (int i = 0; i < 20; i++){
            System.out.println("What is the kitten's name?");
            String kitten = scan.nextLine();
            System.out.println("That kitten's name was " + kitten);
        }
    }
}

答案 3 :(得分:0)

package kittensworld;

import java.util.Scanner;

public class KittensWorld {

    private static final int NUMBER_OF_KITTENS = 20;
    public static void main(String[] args){
        String[] kittenNames = new int[NUMBER_OF_KITTENS];
        Scanner scan = new Scanner(System.in);

        for(int i = 0; i < NUMBER_OF_KITTENS; ++i)
        {
            System.out.println("What is the name of kitten number " + (i+1) + "?");
            kittenNames[i] = scan.nextLine();
        }
    }
}

答案 4 :(得分:0)

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    ArrayList<String> kittenNames = new ArrayList<>();
    int number = 3 ; // number of kitten names ( +1 )

    for (int i = 1; i < number; i++) {
        System.out.printf("What is the %s kitten's name?\n", ordinal(i));
        kittenNames.add(scan.nextLine());
    }
    System.out.println("===============================================");
    for (String name : kittenNames) {
        System.out.println(name);
    }
}

public static String ordinal(int i) {
    String[] sufixes = new String[]{"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"};
    switch (i % 100) {
        case 11:
        case 12:
        case 13:
            return i + "th";
        default:
            return i + sufixes[i % 10];

    }
}