Java while循环中的方法
具体地说,在while循环中有一些逻辑区域不允许程序正确流动。我已经走过了这个循环,它应该会起作用。我遇到的一些错误是,当我输入“0”时,它不会立即退出,换句话说,我必须按两次0,这对我来说没有意义,除非我没有正确理解while循环。另一个错误是在add()方法中,无论我输入什么,它都只告诉我输入的第一个数字。所以我相当肯定这个错误在我的循环中,但我看不出逻辑错误是从哪里来的。任何帮助都将不胜感激,谢谢Java while循环中的方法,java,methods,while-loop,Java,Methods,While Loop,具体地说,在while循环中有一些逻辑区域不允许程序正确流动。我已经走过了这个循环,它应该会起作用。我遇到的一些错误是,当我输入“0”时,它不会立即退出,换句话说,我必须按两次0,这对我来说没有意义,除非我没有正确理解while循环。另一个错误是在add()方法中,无论我输入什么,它都只告诉我输入的第一个数字。所以我相当肯定这个错误在我的循环中,但我看不出逻辑错误是从哪里来的。任何帮助都将不胜感激,谢谢 import javax.swing.JOptionPane; public class
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
这是我认为你不想要的东西:
//Call Input method
input();
numb = input();
做两次会导致两次询问一个数字-但只使用第二个
另外,要非常小心浮点数和相等性。0是一个很好的数字,但其他数字,特别是分数并不是很明显
除此之外,代码似乎还可以
(而且您没有“添加”方法)这是我认为您不想要的:
//Call Input method
input();
numb = input();
做两次会导致两次询问一个数字-但只使用第二个
另外,要非常小心浮点数和相等性。0是一个很好的数字,但其他数字,特别是分数并不是很明显
除此之外,代码似乎还可以
(而且您没有“add”方法)据我所见,您应该在循环中只调用一次“input()”。你打了两次电话——也就是说
input();
numb = input();
拿出第一个“input();”,你就可以开始了
您也不需要执行“sum(add,numb);”两次,所以去掉“sum(add,numb);”行,只需使用“add=sum(add,numb);” 据我所见,您应该在循环中只调用一次“input()”。你打了两次电话——也就是说
input();
numb = input();
拿出第一个“input();”,你就可以开始了
您也不需要执行“sum(add,numb);”两次,所以去掉“sum(add,numb);”行,只需使用“add=sum(add,numb);” 您请求输入,然后将其丢弃
//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();
您应该只在序言中声明numb,而将其余部分留给循环
循环中的问题
查看您的循环,我发现以下问题:
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
由于舍入错误,您可能会得到意外的结果,结果为((a/b*b)-a)
为0.0000000000 1
,这将导致测试(numb!=0)失败。因为现在可以与用户输入进行比较,但是如果要检查计算的输出,请记住浮点计算是不精确的您请求输入,然后将其丢弃
//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();
您应该只在序言中声明numb,而将其余部分留给循环
循环中的问题
查看您的循环,我发现以下问题:
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
由于舍入错误,您可能会得到意外的结果,结果为((a/b*b)-a)
为0.0000000000 1
,这将导致测试(numb!=0)失败。因为现在可以与用户输入进行比较,但是如果要检查计算的输出,请记住浮点计算是不精确的不能将浮点值与绝对值进行比较。舍入错误会使您绊倒。@Johan:除非
parseFloat
方法被破坏(不太可能),否则它将返回一个精确的浮点表示形式零,因此比较不是问题。浮点数精确表示整数,直到有效位用完位为止,因此单精度(23位有效位和一个隐含的额外位)可以精确表示-2^24到2^24之间的整数。不能将浮点数与绝对值进行比较。舍入错误会使您绊倒。@Johan:除非parseFloat
方法被破坏(不太可能),否则它将返回一个精确的浮点表示形式零,因此比较不是问题。浮点数精确地表示整数,直到有效位用完位为止,因此单精度(有23位有效位和一个隐含的额外位)可以精确地表示-2^24到2^24之间的整数。使用未赋值和()很好地找到了。。。我的眼睛根本看不到。。。只是我看了一下,avg()也一样……非常感谢。这真的帮助了我。我的sentinel值现在正常工作,计数正常工作,唯一的问题是sum()方法。我会努力解决这个问题,我想这与我所说的方式有关。例如,如果我输入5 3次,它说我的总和是10,这显然是错误的。你的总和函数很好,问题在其他地方,让我更新答案…很好地找到了未分配的总和()。。。我的眼睛根本看不到。。。只是我看了一下,avg()也一样……非常感谢。这真的帮助了我。我的sentinel值现在正常工作,计数正常工作,唯一的问题是sum()方法。我会努力解决这个问题,我想这与我所说的方式有关。例如,如果我输入5 3次,它显示我的总和是10,这显然是错误的。你的总和函数很好,问题在别处,让我更新答案….除了代码看起来很好之外
。。。不,它不是,它有更多的bug。除此之外,代码似乎很好
。。。不,它不是,它有更多的bug。