[英]Method within a while loop
具體來說,在while循環中,存在某些邏輯區域,該區域不允許程序正確流動。 我已經完成了這個循環,它應該可以工作。 我遇到的一些錯誤是,當我輸入“ 0”時,它並不會立即退出,換句話說,我必須按兩次0,這對我來說是沒有意義的,除非我沒有正確理解while循環。 另一個錯誤是在我的add()方法中,無論我輸入什么,都只會告訴我輸入的第一個數字。 因此,我可以肯定地確定錯誤在我的循環中,但是我看不到邏輯錯誤的來源。 任何幫助,將不勝感激,謝謝。
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是一個很好的數字,但其他數字,尤其是小數則不太明顯...
除此之外,代碼似乎還可以...
(並且您沒有“添加”方法)
您要求輸入然后扔掉
//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();
您只應在序言中聲明麻木,然后將其余的留給循環。
循環中的問題
查看您的循環,我看到以下問題:
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
盡管java允許您像調用過程一樣調用函數(返回值)(不返回任何值,也稱為void
),但只有對返回值不感興趣時才應該這樣做。
在這種情況下,您要兩次調用該函數。
第一個呼叫要求用戶輸入,然后丟棄結果,第二個呼叫再次詢問用戶,然后存儲答案。
由於您是在循環中運行,因此很難將重復與兩次運行的循環區分開。
您永遠不必兩次調用函數,就像在循環中那樣。
您不必預先宣布功能
只需聲明您想發生的事情,java就會做到。
其他事宜
在這種情況下,這不是問題,因為numb
是由用戶直接輸入的。 但是通常,您絕對不應將浮點數與絕對值進行比較。
以下內容不能保證是正確的。
((a/b*b)-a == 0)
由於舍入錯誤,您可能會得到意外的結果,其中((a/b*b)-a)
為0.00000000001
,這將導致您測試(numb!= 0)失敗。
因為您現在要與用戶輸入進行比較,所以現在還可以,但是,如果要檢查計算的輸出, 請記住浮點計算是不精確的!
從我所看到的,您應該只在循環中調用一次“ input()”。 您打過兩次電話-即
input();
numb = input();
取出第一個“ input();” 而且你應該很好走。
您也不需要執行“ sum(add,numb);”。 兩次,因此取出“ sum(add,numb);” 行,只需使用“ add = sum(add,numb);”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.