• 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

find the smallest, largest, and average

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't get my code to work!! Please help. I need to find the smallest, largest, and average. I'm VERY new to all of this, so anything will help.

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, and welcome to the Ranch!

Here are a couple of tips to help you get the most out of the awesome help that's available here:

1. TellTheDetails(⇐click) and ItDoesntWorkIsUseless(⇐click). We need to know exactly what the problem is in order to effectively help you.

2. When posting code, please UseCodeTags(⇐click). I added them for you this time. See how much more readable it is?

So let us know more about what the problem is, and somebody will be happy to help.
 
Angilia Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apologize. I can get it to run however, after the user enters the integers nothing happens... Logically I feel like my code makes sense, So I'm clueless to how to fix it!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Angilia Smith wrote:after the user enters the integers nothing happens... Logically I feel like my code makes sense, So I'm clueless to how to fix it!



You think your code should do one thing. Instead, it's doing something else. Obviously, that means one or more of your assumptions are wrong. So you can either stare at the code until you find your mistake (and once you gain experience, that's perfectly acceptable for certain problems, at least up to a point), or you can observe what your code is actually doing, and compare that to what you expect it to do. Where observation diverges from expectation, that's a sign that will point you toward your bogus assmption.

So add println() calls so that you can see what your code is actually doing--what's being executed and what various significant values are. For example:

 
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
Don't ever write lines like this:



Always do this:

(use whatever brace style you want, but be consistent).

The reason is that someday, you will want to put a println() statement inside the body of your if, or add more logic, or do SOMETHING else....and you will forget that the body is only one line when there aren't braces. And thing will get VERY weird VERY fast.

Further, writing a single line like that is so unusual that when I first looked at it, my thoughts went "Wait, that won't even compile...oh, I see what he did". When you are writing code, you should write it so that it makes sense to everyone. Following common conventions is a REALLY good idea.
 
Angilia Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added the lines and found that its only reading the first number the user inputs. For example, if I input 100 78 45 -3, next always equals 100.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Angilia Smith wrote:I added the lines and found that its only reading the first number the user inputs.



Hmmm... So it seems to be ignoring the code you wrote where you tell it to read more. Point out where you're telling it to read more, so that we can help you figure out why it's not working.
 
Angilia Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you're asking. I thought that while in a loop it would continue to the next number until it were no longer true. So in this case when next <= 0.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Angilia Smith wrote:I'm not sure what you're asking.



I'm asking why you expect it to process multiple user inputs when you're only ever telling it to read one of them.

I thought that while in a loop it would continue to the next number until it were no longer true. So in this case when next <= 0.



Correct, except that since your loop is while(next >= 0), it will keep going as long as next is >= 0, which means it will stop when x < 0, not when x <= 0. That's not your problem here, although it's something you'll need to pay attention to whenever you write a loop condition like that.

You have two problems that I can see. One is this:


Putting that semicolon there is the same as doing this:


You need to get rid of the semicolon after the while(...).

Your other problem, after you fix that, is that your loop body will either execute zero times or infinitely many times. This is what I was trying to hint at in my previous reply. You want the loop to execute as many times as there are user inputs, right? That means you have to read the user input for each execution of the loop, yes? But you're only reading one user input, and you use that same value for every iteration of the loop.

So if the value is sufficient for the loop to execute once, since you never change that value (because you're never reading any more user inputs), that means the loop will never stop executing.

If you want to process one user input per loop iteration, then you need to read one user input per loop iteration, but you're not doing that.
 
Paper jam tastes about as you would expect. Try some on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic