简体   繁体   English

while循环中的方法

[英]Method within a while loop

Specifically, in the while loop there is some logic area that is not allowing the program to flow correctly. 具体来说,在while循环中,存在某些逻辑区域,该区域不允许程序正确流动。 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. 我遇到的一些错误是,当我输入“ 0”时,它并不会立即退出,换句话说,我必须按两次0,这对我来说是没有意义的,除非我没有正确理解while循环。 The other error is in my add () method, what ever I enter it only tells me the first number I inputted. 另一个错误是在我的add()方法中,无论我输入什么,都只会告诉我输入的第一个数字。 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. 任何帮助,将不胜感激,谢谢。

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

This is something that I dont think you want: 这是我认为您不想要的东西:

        //Call Input method    
        input();
        numb = input();

Doing it twice results in asking for a number twice - but only using the second... 进行两次会导致要求输入两次-但只能使用第二个...

Also, be very careful about floating point numbers and equality. 另外,请务必注意浮点数和相等性。 0 is a good number, but other numbers, especially fractions are not so obvious... 0是一个很好的数字,但其他数字,尤其是小数则不太明显...

Other than that, the code seems OK... 除此之外,代码似乎还可以...

(and you don't have an "add" method) (并且您没有“添加”方法)

You ask for input and then throw it away 您要求输入然后扔掉

//in the initialization you do:...
float numb = input();//used to store the value of Input() method
//and in the loop you do it again, overwrite the first input
numb = input();

You should only declare the numb in the preamble and leave the rest to the loop. 您只应在序言中声明麻木,然后将其余的留给循环。

Problems in the loop 循环中的问题

Looking at your loop I see the following problems: 查看您的循环,我看到以下问题:

while (numb != 0) {//Begin for loop
    count += 1;
    //Call Input method    
    input();                  <<-- 1. should be removed, input goes nowhere 
    numb = input();
    //Method to find the sum of all the numbers inputed
    sum(add,numb);            <<-- 2. should be removed, output goes nowhere
    add = sum(add,numb); 
    //Used to find the average of all the numbers entered (sum / count)
}//End for loop
avg(add,count);               <<-- 3. why do you keep repeating??
average = avg(add,count);//used to store the average of the numbers

Whilst java allows you to call functions (return a value) as if they're procedures (returns nothing aka void ), you should only do this if you're not interested in the return value. 尽管java允许您像调用过程一样调用函数(返回值)(不返回任何值,也称为void ),但只有对返回值不感兴趣时​​才应该这样做。
In this case you're calling the function twice. 在这种情况下,您要两次调用该函数。
The first call asks the user for input and then throws the result away, the second call ask the user again and then stores the answer. 第一个呼叫要求用户输入,然后丢弃结果,第二个呼叫再次询问用户,然后存储答案。

Because you are running in a loop, it's hard to differentiate the duplication from the loop running twice. 由于您是在循环中运行,因此很难将重复与两次运行的循环区分开。
You should never have to call a function twice, like you're doing in your loop. 您永远不必两次调用函数,就像在循环中那样。

You don't have to pre-announce your functions 您不必预先宣布功能
Just state what you want to happen and java will do it. 只需声明您想发生的事情,java就会做到。

Other issues 其他事宜
In this case it's not a problem, because numb is directly inputted by the user. 在这种情况下,这不是问题,因为numb是由用户直接输入的。 But in general you should never compare a float against an absolute value. 但是通常,您绝对不应将浮点数与绝对值进行比较。
The following is not guaranteed to be true. 以下内容不能保证是正确的。

((a/b*b)-a == 0)

Because of rounding errors you can get unexpected results, where the outcome is of ((a/b*b)-a) is 0.00000000001 which will cause you test (numb != 0) to fail. 由于舍入错误,您可能会得到意外的结果,其中((a/b*b)-a)0.00000000001 ,这将导致您测试(numb!= 0)失败。
Because you're comparing against user input it's fine for now, but if you're checking the output of a calculation, remember that floating point calculations are inexact! 因为您现在要与用户输入进行比较,所以现在还可以,但是,如果要检查计算的输出, 请记住浮点计算是不精确的!

From what I am seeing, you should only call "input()" once in your loop. 从我所看到的,您应该只在循环中调用一次“ input()”。 You are calling it twice - ie 您打过两次电话-即

          input(); 
          numb = input();

Take out the first "input();" 取出第一个“ input();” and you should be good to go. 而且你应该很好走。

You also don't need to execute "sum(add,numb);" 您也不需要执行“ sum(add,numb);”。 twice, so take out the "sum(add,numb);" 两次,因此取出“ sum(add,numb);” line and just use "add = sum(add,numb);". 行,只需使用“ add = sum(add,numb);”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM