• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

system exit and product of ints

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

I am trying to multiple integers so that if the input is 1 2 3 the product of the outputs would be 1*1,2*2,3*3,1*2,1*3,2*3. I also need the code to ignore negative numbers and exit at 0. Also, I have set up a menu and I cannot figure out how to loop it back to the menu after the program runs. Thank you in advance for any help!

This is the code I have so far:
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well...you didn't ask a specific question, so this will be very general.

Slow down. it looks like you're trying to do everything all at once. The key thing to remember is that much of what you are trying to do is independent from from everything else. What I mean by that is that each of these are components you need to get to work, but none of them rely on anything else:

1) get data from user
2) generate all the pairs of numbers to multiply
3) multiply two number together
4) print the results
5) loop back to the menu

You should pick exactly ONE of these things, and get it to work without even thinking about anything else. So you may be saying "well, how can I do #2 without doing #1, and what is the point of doing #1 if I don't have #2?"

The answer is you fake it. each of these items above should be their own method (well...maybe not #5). So, you write a method that takes a list of number and generates all the pairs. You would need to pass this list to it somehow - as an argument. So once you realize that, it doesn't matter how you generate that list you pass to it. You can hard code it, you can read it from a file, you can get it from the user or from a database...The important thing is you write the method for #2 such that it doesn't matter. Then, when you know #2 works, you can use it all you want as you code #3.

If you have a specific question, please feel free to ask...
 
Angela Hill
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would I set up the pairs if I only take in one array?

for example:


my input is 1 2 3
and my output is
1 4 9...
How do i pair up 2*3, 1*3 and 1*2 without taking in a second variable?
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now we're getting into what programming is all about.

How did you determine that given "1 2 3" you should end up with the pairs (2,3), (1,3) and (1,2)? What pairs would you come up with if you were given "1 2 3 4 5 6"? I don't want an actual answer/list. I want you to tell me how to figure out what they'd be - in English. If it's easier, start by telling me how many pairs you should have with a list of six elements.

also, this may help...if I was given "1 2 3 4" the pairs would look like this (I think):

(1,2)
(1,3)
(1,4)
(2,3)
(2,4)
(3,4)

Do you notice a pattern there? Data analysis is an important skill to work on. Look at what your trying to get, and then figure out what the patterns are...what might be related to something else, what might be a counter, what might be a red herring...


note: something to keep in teh back of your mind...what if the user inputs (1 1 1 1 2)...i.e. a repeated value. We can do whatever you want, but you will have to decide if you want to de-dup the list, treat each element as distinct regardless, tell the user dups aren't allowed, or something else.
 
reply
    Bookmark Topic Watch Topic
  • New Topic