-2

到目前为止,我的代码如下所示:

    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 次?

4

5 回答 5

2

使用包含数据的for循环和数组:

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.

于 2013-10-10T14:57:47.890 回答
2

I think you need to look up this tutorial about this proper loop called 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();
}
于 2013-10-10T14:59:30.973 回答
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);
        }
    }
}
于 2013-10-10T14:57:32.000 回答
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();
        }
    }
}
于 2013-10-10T15:07:55.983 回答
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];

    }
}
于 2013-10-10T15:09:31.347 回答