خرید بک لینک

Vote count: 0

I am super new to Java and having trouble with getting the output right. I am setting up an array and then asking the user to give me the number of the message they want displayed. So I see what it is doing. If I enter the number 5 expecting to have it display the 5th line, it will not do it but have me instead enter 5 numbers and then display the 5th line. I caot figure out why it is doing this. I'm also supposed to wrap this whole thing in a method called shoutOutCaedMessage() and I'm not sure how to do this since I have already done it without that. I literally just started leaing Java like two weeks ago. Thanks for any help. I'm going to continue testing and trying but thought I may get a bit of assistance.

import java.util.Scaer; //use java api scaer class

class MessageArray {

public static void main(String[] args) {
    Scaer s = new Scaer(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;

    **shoutOutCaedMessage()**; {
    //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++;
        }
}

The output I am getting now is:

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

asked 16 secs ago

برچسب: نویسنده: استخدام کار تاريخ: شنبه 29 خرداد 1395 ساعت: 0:02

صفحه بندی