我在Java中的新蜂數...所以我有一個任務,我能堅持有人幫助我爪哇 - 讀取和確定整數
我必須寫一個程序,讀取數目不詳整數,確定讀取了多少個正值和負值,並計算輸入值的總和和平均值(不計零)。該程序應結束,當用戶輸入0
採樣運行:
Enter an int value (the program exits if the input is 0: 1 3 -1 2 0
The number of positives is 3
The number of negatives is 1
The total is 5
The average is 1.25
我寫的東西是...的邏輯似乎是正確的,但它不工作...
的我寫的代碼是
public class AverageNumbers {
public static void main(String[] args)
{
int data = 0;
int positive = 0;
int negative = 0;
int count = 0;
while (data !=0)
{
String strdata = JOptionPane.showInputDialog ("Enter the integer The input ends if the value is 0") ;
data = Integer.parseInt(strdata);
if (data < 0 || data > 0){
count++;
if (data >0)
positive++;
else if (0<data)
negative++;
count = count + data;
count++;
}
String strOutput = "The number of positives is " + positive;
JOptionPane.showMessageDialog(null, strOutput);
strOutput = "The number of negatives is " + negative;
JOptionPane.showMessageDialog(null, strOutput);
strOutput = "The number of total is " + count ;
JOptionPane.showMessageDialog(null, strOutput);
strOutput = "The number of average is " + count/data ;
JOptionPane.showMessageDialog(null, strOutput);
}
}
您是否有問題嗎? –
你從data = 0開始,所以你永遠不會進入while循環。 – Jems