在用户输入

时间:2016-06-17 16:48:22

标签: java arrays user-input

我是Java的新手,无法正确获取输出。我正在设置一个数组,然后要求用户给我他们想要显示的消息的编号。所以我看到它在做什么。如果我输入数字5,希望它显示第5行,它将不会这样做,而是让我输入5个数字,然后显示第5行。我无法弄清楚它为什么会这样做。我也应该在一个名为shoutOutCannedMessage()的方法中包装这整个事情,我不知道如何做到这一点,因为我已经没有这样做了。我两个星期前就开始学习Java了。谢谢你的帮助。我将继续测试并尝试,但我想我可能会得到一些帮助。

import java.util.Scanner; //use java api scanner class

class MessageArray {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        //create an array of strings with 10 objects
        String[] array = new String[10];

        //create a counter to go through each message
        int counter = 0;
        int response;

        **shoutOutCannedMessage()**; {
        //start looping through each element in the array asking user for input 
        while (counter < array.length) {
            System.out.print((counter+1) + ": Please enter message: ");
            array[counter] = s.nextLine();
            counter++;
        }
        }


        System.out.print("Please enter the number of the message you would like displayed: ");
        counter=0;
        while (counter < array.length) {
                        response = s.nextInt();
            if (response == counter){
                System.out.println("Your selected value is " + array[counter]);
                counter = array.length+1;
            }
            counter++;
            }
    }

我现在得到的输出是:

1: Please enter message: fdas
2: Please enter message: fgd
3: Please enter message: sdf
4: Please enter message: fh
5: Please enter message: af
6: Please enter message: dsf
7: Please enter message: h
8: Please enter message: fs
9: Please enter message: GDFS
10: Please enter message: FDAS
Please enter the number of the message you would like displayed: 
4
6
5
4
4
Your selected value is af

2 个答案:

答案 0 :(得分:1)

这有效,你不需要另一个循环: 如果它高于10或低于1,可能会进行一些检查。

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);

    //create an array of strings with 10 objects
    String[] array = new String[10];

    //create a counter to go through each message
    int counter = 0;

    //start looping through each element in the array asking user for input 
    while (counter < array.length) {
        System.out.print((counter+1) + ": Please enter message: ");
        array[counter] = s.nextLine();
        counter++;
    }


    System.out.print("Please enter the number of the message you would like displayed: ");

    int response = s.nextInt();
    response-= 1;          
   System.out.println("Your selected value is " + array[response]);

}

答案 1 :(得分:0)

所以我最终按照上面的帮助解决了我的问题:

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);

    //create an array of strings with 10 objects
    String[] array = new String[10];

    //create a counter to go through each message
    int counter = 0;
    int response;


    //start looping through each element in the array asking user for input 
    while (counter < array.length) {
        System.out.print((counter+1) + ": Please enter message: ");
        array[counter] = s.nextLine();
        counter++;
    }

    System.out.print("Please enter the number of the message you would like displayed: ");
    counter=0;
    response = s.nextInt();
    System.out.println("Your selected value is " + array[response-1]);
    s.close();

        }   

}