0

I don't have any idea how to display the largest and smallest number after the user enter -1.

Note: I also have to display the sum of all of this number and the average of this sum.

Here's my code so far:

public static void main(String[] args) {
    // store
    int cnt = 0;
    int acumulator = 0;
    int larger = 0;
    int smaller = 0;
    int number;
    // inputs
    System.out.println("enter the number all the number");
    Scanner kb = new Scanner(System.in);
    number = kb.nextInt();
    // loop
    while (number != -1) {
        acumulator += number;
        number = kb.nextInt();
        cnt++;//
    }
    double average = (acumulator / cnt);
    System.out.println(" The total of your number is=" + acumulator);
    System.out.println(" The average of your number is=" + average);
}
4
  • Might I ask what the difference between cnt and count is and why only one is used? :-) Commented Nov 6, 2011 at 23:04
  • cnt is to know how many number have been entered, count is an error. Commented Nov 6, 2011 at 23:10
  • I see that. But what is count? :-) Commented Nov 6, 2011 at 23:12
  • count doesn't do anything. It is my mistake Commented Nov 6, 2011 at 23:14

4 Answers 4

1

Seems like schoolwork, but what you could do is making a var and checking in your while if the input number is higher or lower then the saved var.

if(input > max)
  max = input;

And

if(input < min)
  min = input;
Sign up to request clarification or add additional context in comments.

3 Comments

It looks like he already has them, but they're named larger and smaller; they're also unused.
Yeah i see, but its more about the idea :)
Thank you so much. the homework is done,but can you explain the logy of this situation or refer me to a wed-side where I can learn this better. My book has a example but it doesn't explain this well and it is too short.
1

I would make the following changes:

  • use a for loop instead of a while loop (you need intialization, condition and iteration)
  • use the JDK's API more - Math.min() and Math.max()
  • spell "accumulator" correctly
  • remove all variables you are not using (cnt)

Try this:

public static void main(String[] args) {
    int accumulator = 0;
    int largest = Integer.MIN_VALUE;
    int smallest = Integer.MAX_VALUE;
    int count = 0;
    // inputs
    System.out.println("enter a number (-1 to end)");
    Scanner kb = new Scanner(System.in);
    for(int number = kb.nextInt(); number != -1; number = kb.nextInt()) {
        count++;
        accumulator += number;
        largest = number > largest ? number : largest;
        smallest = number < smallest ? number : smallest;
    }
    double average = (accumulator / count);
    System.out.println(" The total of your numbers is=" + accumulator);
    System.out.println(" The average of your numbers is=" + average);
    System.out.println(" The largest of your numbers is=" + largest);
    System.out.println(" The smallest of your numbers is=" + smallest);
}

FYI: Math.min/max could be used instead of the ternary statements, but the above is the simplest java that will achieve the result.

4 Comments

the main problem here is that my teacher has never taught how to use Math.min() and Math.max(), and she may say that it isn't what she wants.
Yeah its a good piece of code, but i guess for a beginner it might be confusing.
OK - I've removed the Math.min etc and replaced with ternaries
But ternaries are also confusing. "if" statements would be the way to go for clear (but not so elegantly compact) beginner code.
0

Basically you need to check each all numbers with each other.Lets say we have numbers 1,2 and 3

here is the code:

public static void main(String[] args){
    int one=1;
    int two=2;
    int three=3;
    if(three>one){
     if(three>two){
       System.out.println("three is biggest");
     }
   }
   if(two>one){
    if(two>three){

   }
}

etc etc.

I think you got idea

Comments

-1

You need to use an "if" statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.