Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27
  1. #21
    vicu1 is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default 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?

  2. #22
    AlphaSupport Guest

    Default 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. ;)

  3. #23
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: smallest number and largest number using while and if statements

    Quote Originally Posted by AlphaSupport View Post
    The "noNumbersYet= false;" is at the wrong place, it will never get executed. ;)
    Even worse, the "noNumbersYet= true;" is also at the wrong place, it is always executed and ruins the entire program logic. ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #24
    AlphaSupport Guest

    Default 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
    Java Code:
    noNumbersYet = true; 
    if (noNumbersYet){ 
    smallest= usernumber;
    }else if (usernumber < smallest){
    smallest= usernumber;
    noNumbersYet= false;
    block is placed wrong.

  5. #25
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: smallest number and largest number using while and if statements

    Quote Originally Posted by AlphaSupport View Post
    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
    Java Code:
    noNumbersYet = true; 
    if (noNumbersYet){ 
    smallest= usernumber;
    }else if (usernumber < smallest){
    smallest= usernumber;
    noNumbersYet= false;
    block is placed wrong.
    Yep, my guess is that the OP blindly copied my code snippet without thinking and set that variable to true also without thinking; I guess s/he did think that it'd work ... ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #26
    vicu1 is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default 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.

  7. #27
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: smallest number and largest number using while and if statements

    Quote Originally Posted by vicu1 View Post
    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.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Find the Biggest Number + Smallest
    By d4v1dz in forum New To Java
    Replies: 19
    Last Post: 10-26-2011, 06:43 AM
  2. How can I find out the largest number ... ?
    By TheTypicalStudent in forum New To Java
    Replies: 14
    Last Post: 05-03-2011, 11:09 AM
  3. Find the second largest number in the array
    By radhi16 in forum New To Java
    Replies: 5
    Last Post: 01-13-2011, 05:08 PM
  4. smallest number java program
    By elmo in forum New To Java
    Replies: 9
    Last Post: 11-18-2010, 09:29 PM
  5. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 06:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •