Note that there are some explanatory texts on larger screens.

plurals
  1. POMethod within a while loop
    primarykey
    data
    text
    <p>Specifically, in the while loop there is some logic area that is not allowing the program to flow correctly. I have walked through this loop and it should work. Some of the errors I am having is when I enter "0" it doesn't exit right away, other words I have to press 0 twice which doesn't make sense to me unless I am not comprehending the while loop correctly. The other error is in my add () method, what ever I enter it only tells me the first number I inputted. So I am fairly certain the error is in my loop, but I can't see where the logic error is coming from. Any help would be appreciated, thank you.</p> <pre><code>import javax.swing.JOptionPane; public class RandyGilmanP2 {//Begin class public static void main(String[] args) {//Begin main JOptionPane.showMessageDialog(null, "Hello Welcome to Sum and Average" + "\n of a Number Calculator Program" + "\n By: Randy Gilman"); //Declare variables float add = 0;//used to store the sum of the numbers inputed float numb = input();//used to store the value of Input() method float average; int count = 0;// Used as a counter variable //Loop that will be controlled by a sentenil value while (numb != 0) {//Begin for loop count += 1; //Call Input method input(); numb = input(); //Method to find the sum of all the numbers inputed sum(add,numb); add = sum(add,numb); //Used to find the average of all the numbers entered (sum / count) }//End for loop avg(add,count); average = avg(add,count);//used to store the average of the numbers Results(count,add,average); }//End Main public static float input(){//Begin Method //Will keep gathering input from user until input is equal to 0 String NumberString = JOptionPane.showInputDialog("Enter a floating point number" + "(The program ends when 0 is entered):"); //Convert string to float float number = Float.parseFloat(NumberString); return number; }//End Method public static float sum(float sum, float numb2){//Begin method //Add number to the previous number to compute total sum += numb2; if (sum &gt; 100) JOptionPane.showMessageDialog(null, "***WARNING***" + "\n The sum of your numbers exceed 100"); return sum; }//End Method public static float avg(float num1, float num2){ //Declare variables float result; //Preform calculation of average result = num1 / num2; return result; }//End Method public static void Results(int counter, float addition, float aver){//Begin method //Declare variables JOptionPane.showMessageDialog(null,"The total amount of numbers you entered are: " + counter); JOptionPane.showMessageDialog(null,"The sum of the numbers you have entered is: " + addition); JOptionPane.showMessageDialog(null,"The average of the numbers you have entered is: " + aver); }//End Method }//End Class </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload