尝试在用户输入后的数组中显示某个字符串



我是Java的新手,并且在正确获取输出方面遇到麻烦。我正在设置一个数组,然后要求用户给我他们想要显示的消息的数量。所以我明白了它在做什么。如果我输入数字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

这有效,您不需要另一个循环:也许有些检查是否更高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]);
}

,所以我最终做了以下每个帮助以解决我的问题:

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();
        }   

}

相关内容

  • 没有找到相关文章