-2

Write a program that always asks the user to enter a number. When the user enters the negative number -1, the program must stop requesting the user to enter a number. The program must then calculate the average of the numbers entered exclusive of the -1. Make use of the while loop repetition structure to implement the program.

This is my solution, but the problem is that I cant exclude the negative number from the sum. Please help

Import javax.swing.*;
public class whileloop
{
    public static void main (String [] args ) 

    {
       int i =0, tot = 0;
       int count = 0;
       double avg = 0.0;
       while(i != -1) 
       {
           i = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter a number");
           if(i != -1)
           {
              tot += i; 
              count++;
              avg = sum/count;
              System.out.println("Sum: " + sum + "\t\t" + "Average: " + avg); 
           }
       }
       System.out.println();
    }
}
vefthym
  • 7,422
  • 6
  • 32
  • 58

4 Answers4

0
    Import javax.swing.*;
    public class whileloop
    {
        public static void main (String [] args )



       {
           int i =0, tot = 0;
           int count = 0;
           double avg = 0.0;
           while(true) 
           {
           i = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter a number");
           if(i != -1)
           {
              tot += i; 
              count++;

           }else{
            avg = tot/count;
              System.out.println("Sum: " + tot+ "\t\t" + "Average: " + avg);
break;
         }
       }
       System.out.println();
    } }
Lukesoft
  • 953
  • 1
  • 12
  • 31
0
Import javax.swing.*;
public class whileloop{
  public static void main (String [] args ) {
    int i =0, tot = 0;
    int count = 0;
    double avg = 0.0;
    while(i >= 0){
      i = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter a number");
      if(i >= 0){
        tot += i; 
        count++;
      }
   }
   avg = sum/count;
   System.out.println("Sum: " + sum + "\t\t" + "Average: " + avg); 
} 

in short:

while ( bigger then 0){
  read number
  check if positive number{
    add number tot total for the average
  }
}
calculate average
print average result

You do also want the average calculation to be outside the while-loop for better readable code.

W vd L
  • 625
  • 9
  • 26
0

First, why are you using a JOptionPane and making your life harder? There is no need to use a JOptionPane for a simple assignment such as this. Second, the calculation for the average should be outside the while loop. If you keep it inside, you risk the program throwing a runtime exception due to calculation problems. Here is what your code should look like from the while loop to the println statement. I cleaned up your code so it's more streamlined.

while (i > 0) {
    tot += i;
    count++;
}

int average = tot / count;
System.out.println("Sum: " + sum + "\t\t" + "Average: " + avg); 
PMJava
  • 1
0

What you describe is logically correct, i.e., it satisfies your requirements.

The only problems that I see are:

  1. You print the sum and avg at every iteration (not necessary).
  2. You should cast the sum to double, when calculating the avg (see this post).
  3. You should use tot instead of the undefined sum (as Gyro Gearless commented).

Thus, my suggested solution (assuming that you need the graphics) is:

Import javax.swing.*;
public class whileloop
{
  public static void main (String [] args ) 

  {
   int i =0, tot = 0;
   int count = 0;
   double avg = 0.0;
   while(i != -1) 
   {
       i = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter a number");
       if(i != -1)
       {
          tot += i; 
          count++;              
       }
   }
   avg = (double)tot/count;
   System.out.println("Sum: " + tot + "\t\t" + "Average: " + avg); 
  }
}
Community
  • 1
  • 1
vefthym
  • 7,422
  • 6
  • 32
  • 58