Results 21 to 27 of 27
- 11-13-2011, 11:41 PM #21
Member
- Join Date
- Nov 2011
- Posts
- 19
- Rep Power
- 0
Re: smallest number and largest number using while and if statements
ok so i tryed doing it and i thought it would work but it didnt. this program is due tomorrow so i am getting angry too becuase i have tried alot of things .
my new code is:
package whilesmalllarge;
import TerminalIO.KeyboardReader;
public class Smalllarge {
public static void main(String[] args) {
KeyboardReader reader = new KeyboardReader();
double usernumber = 0 ;
double i = 0 ;
double count;
double highest = 0;
double smallest = 0;
boolean noNumbersYet;
count =reader.readInt(" How long is the list?");
while( i < count){
usernumber = reader.readInt(" input a number:");
i++;
}
noNumbersYet = true;
if (noNumbersYet){
smallest= usernumber;
}else if (usernumber < smallest){
smallest= usernumber;
noNumbersYet= false;
}
System.out.print("The smallest number is :"+ smallest);
}
}
do i just have to add something else after it? if so what?
- 11-14-2011, 07:13 AM #22AlphaSupport Guest
Re: smallest number and largest number using while and if statements
First of all, when posting code to the forums, please surround it with [code]...[/code]. That way your post will be more readable.
The "noNumbersYet= false;" is at the wrong place, it will never get executed. ;)
- 11-14-2011, 07:41 AM #23
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
Re: smallest number and largest number using while and if statements
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-14-2011, 07:50 AM #24AlphaSupport Guest
Re: smallest number and largest number using while and if statements
Lol correct, didn't even look that further. But there are more things wrong in the current version. ;)
(And i don't even speak Java)
Edit:
Actually it looks like the whole
block is placed wrong.Java Code:noNumbersYet = true; if (noNumbersYet){ smallest= usernumber; }else if (usernumber < smallest){ smallest= usernumber; noNumbersYet= false;
- 11-14-2011, 08:04 AM #25
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
Re: smallest number and largest number using while and if statements
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-14-2011, 02:17 PM #26
Member
- Join Date
- Nov 2011
- Posts
- 19
- Rep Power
- 0
Re: smallest number and largest number using while and if statements
Ok I will be honest yes I guess I did basically copy your code. I did think about how it works and yes it made sense to me. However the Boolean thing is quite new to me. I remember that my teacher talked maybe a little about it but we never used it in our code so I didn’t really understand that part. My code now luckily works. I basically played around with it and I finally got it into the right order. But can someone please explain to me how the Boolean works. Like what happens when you set it to true and when you set it to false. I just want to know for future programs that I will do. You can close this threat after that.
- 11-14-2011, 02:22 PM #27
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
Re: smallest number and largest number using while and if statements
A boolean variable can take one of two values: true or false; their value can be used in if-tests, just like any other boolean value; nothing special happens when you set the value of a boolean variable, nothing explodes nor do penguins pop up on top of your television set, nothing.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Find the Biggest Number + Smallest
By d4v1dz in forum New To JavaReplies: 19Last Post: 10-26-2011, 06:43 AM -
How can I find out the largest number ... ?
By TheTypicalStudent in forum New To JavaReplies: 14Last Post: 05-03-2011, 11:09 AM -
Find the second largest number in the array
By radhi16 in forum New To JavaReplies: 5Last Post: 01-13-2011, 05:08 PM -
smallest number java program
By elmo in forum New To JavaReplies: 9Last Post: 11-18-2010, 09:29 PM -
Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11-03-2010, 06:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks