Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计算循环中的浮点数之和?_Java_Methods_While Loop - Fatal编程技术网

Java 计算循环中的浮点数之和?

Java 计算循环中的浮点数之和?,java,methods,while-loop,Java,Methods,While Loop,好的,我只是问了一个关于循环中的方法的问题。我得到了大部分答案;但是,我的sum()方法仍然不起作用。例如,我把5放在数字3上,这三个数字的和应该是15。因此,在整个循环的某个地方,一个数字并没有被添加到变量add中。任何帮助都将不胜感激,谢谢 // File: RandyGilmanP2.java // Author: Randy L. Gilman // Date: 10/5/2013 // Purpose: Design and implement a Java pr

好的,我只是问了一个关于循环中的方法的问题。我得到了大部分答案;但是,我的sum()方法仍然不起作用。例如,我把5放在数字3上,这三个数字的和应该是15。因此,在整个循环的某个地方,一个数字并没有被添加到变量add中。任何帮助都将不胜感激,谢谢

// File:     RandyGilmanP2.java
// Author:   Randy L. Gilman
// Date:     10/5/2013
// Purpose:  Design and implement a Java program that will gather a group of 
//           floating point numbers and determine the sum and average of the 
//           data entered. The program should use separate methods for inputting 
//           the data, calculating the sum, calculating the average, and 
//           displaying the results. A sentinel value should be used to indicate 
//           the user has completed entering their numbers.  The output should 
//           display a message that includes the count of the numbers entered, 
//           the sum of the numbers and the average of the numbers. If the sum 
//           of the numbers is greater than 100, a warning message should be 
//           displayed indicating “values have exceeded a sum 100”.

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    
             numb = input();
            //Method to find the sum of all the numbers inputed
            add = sum(add,numb); 
            //Used to find the average of all the numbers entered (sum / count)
        }//End for loop
        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

您正在从输入中读取下一个数字,然后再添加它。尝试:

    while (numb != 0) {//Begin for loop
        count += 1;
        //Method to find the sum of all the numbers inputed
        add = sum(add,numb);             
        //Call Input method    
        numb = input();
    }

您正在从输入中读取下一个数字,然后再添加它。尝试:

    while (numb != 0) {//Begin for loop
        count += 1;
        //Method to find the sum of all the numbers inputed
        add = sum(add,numb);             
        //Call Input method    
        numb = input();
    }
那堂课一团糟,我为此感到不安,请用这个


那个类乱七八糟,我对此感到不安,请用这个。

你说你在一个只有2个参数的函数中输入了5、3次,我遗漏了什么吗?我建议你使用BiGDecimal来处理decimal。是的,我实际上在add=sum(add,count)之后放了一个print命令;语句,它显示第一次运行循环时,没有记录某些值。您的问题中有很多不必要的代码。另外,最好不要让我们猜测我们应该为您的程序提供什么输入,最好是硬编码的,并且还应该提供实际输出。对不起,我是这个网站的新手,我将解决未来的问题,谢谢。您说您在一个只有2个参数的函数中输入了5、3次,我遗漏了什么吗?我建议你使用BiGDecimal来处理decimal。是的,我实际上在add=sum(add,count)之后放了一个print命令;语句,它显示第一次运行循环时,没有记录某些值。您的问题中有很多不必要的代码。另外,最好不要让我们猜我们应该给你的程序什么输入,最好是硬编码的,实际输出也应该提供。对不起,我是新来这个网站的,我会解决未来的问题,谢谢。老兄,你是我整个周末都在处理的救命恩人,是的,它解决了这个问题,我只是不明白为什么它不能以另一种方式工作,但我稍后会研究它//用于存储输入()方法的值。你是我整个周末都在处理的救命恩人,是的,这解决了它。我只是不明白为什么它不能以另一种方式工作,但我稍后会研究它。因为这个:float numb=Input()//用于存储Input()方法的值