简体   繁体   中英

Filling Java 2D array input with loop

I am pretty new to JAVA and I need help getting some sort of nested loop I think for a users input and placing the input in a array. There is also a sentinel of -1. I know that it is frowned upon to do homework for others and that is not what I am asking at all. I would just like someone to help explain this not write the code for me. I just need help starting out. I can't figure out the first part of this program:

I have to use a 2D array of [4][5] with the 4 being the number of sales people, and the 5 being the number of the product they sold. I have to ask the user for the person number (1-4) and then ask for the product sold (1-5). I then have to ask for the dollar value of the product.

I can't figure out how to ask the user for the person number and then the product and dollar value without having a big nested loop that looks bad and uses the same code over and over. I know there is a simpler way to do this. Any help would be appreciated. Thank you in advance.

This is what my output is supposed to look like. I just can't figure out a simple way to loop it around and add the elements to the array.


Output: Your output should reflect the array in tabular format ALONG with cross-sectional totals. For example, your program should look something like this:

Enter Sales Person Number (1-4) or -1 to quit and view data:

1

Enter the Product Number (1-5):

1

Enter the dollar value:

1000

Enter Sales Person Number (1-4) or -1 to quit and view data:

2

Enter the Product Number (1-5):

1

Enter the dollar value:

2000

Enter Sales Person Number (1-4) or -1 to quit and view data:

2

Enter the Product Number (1-5):

2

Enter the dollar value:

500

Enter Sales Person Number (1-4) or -1 to quit and view data:

-1

What part doesn't work for you? I suppose you get the user inputs through the use of something like Scanner ?

Next, the 2 inputs for your 2 dimensional array you can store in temporary variables and then write the 3rd input to that position in the array.

Then at the input of -1 you can call the data back through two for loops nested in each other.

But again, without an example or more clarification I'm not sure what you need exactly and I can't know what to elaborate on.

I think the question is more about the way to do the process than to fill the array. I may be wrong but when you talk about a loop it is the loop that print the entries isn't it?

If it is, then in console mode you have no other choice than doing this loop. But you can definitely do something clean with it. Consider looking at State Machine . It may be complicated but I think it can be understood pretty well.

If you want to read data till the user quit with -1 , you can do it by just making one infinite loop with break on -1 for Sales Person Number . If there should be a limit to number of inserted records, add a counter.

while(true){
    //Read Sales Person Number
    if(salesPersonNumber < 0)
        break;
    //Read Product and value
}

With counter

int counter = 5;
while(counter-->0){
    //code
}

You can also use for loops in both cases of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM