2014-02-13 45 views
-2

我在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); 
    } 
} 
+1

您是否有問題嗎? –

+1

你從data = 0開始,所以你永遠不會進入while循環。 – Jems

回答

0

+1企圖和寫作幾乎工作的代碼:)

正如其他人指出你的代碼有哪些需要修復一些bug。

  1. 你分配到的數據應該進入 而(數據)循環之前至少發生一次。
  2. 你不需要在遞增計數之前檢查輸入的值,如果 它是0,那麼你將不會進入循環。否則,您將不得不 增量計數。
  3. 爲計算總數和平均數,你需要一個多變量

你使用任何IDE使用? 使用IDE(Eclipse是最好的之一)並逐步調試代碼來檢查發生了什麼。 Google針對如何使用IDE和調試。

以下是供您參考的工作代碼。

import javax.swing.*; 

public class AverageNumbers { 

    public static void main(String[] args) 
    {   
      int data = 0; 
      int positive = 0; 
      int negative = 0; 
      int count = 0; 
      int total = 0; 
      double avg = 0; 


      String strdata = JOptionPane.showInputDialog ("Enter the integer The input ends if the value is 0") ; 
      data = Integer.parseInt(strdata); 

      while (data !=0) 
      { 
       if (data >0) { 
        positive++; 
       } else { 
        negative++; 
       }      
       total = total + data; 
       count++; 

       strdata = JOptionPane.showInputDialog ("Enter the integer The input ends if the value is 0") ; 
       data = Integer.parseInt(strdata); 
      } 


      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); 
      avg = total * 1.0 /count ; 
      strOutput = "The number of average is " + avg; 
      JOptionPane.showMessageDialog(null, strOutput); 
      System.exit(0); 
    } 
} 
0

那麼,第一個問題就在這裏。你的數據初始化爲0:

int data = 0; 

那麼你的循環條件檢查數據= 0:

while (data !=0) { 
     ... 
} 

由於數據被分配0,環路從來沒有進入。

0

您應該添加'sum'int變量並更改此行

count = count + data;

用這個

sum + = data;

還將您的while-loop更改爲do-while循環。

1

在您的while循環條件中,您正在檢查是否data !=0返回false並且while循環代碼塊未執行,因爲在程序開始時數據被設置爲int data = 0;

有2個到你的問題簡單的解決方案:

  1. 集數據比0
  2. 使用do...while環以外的東西,見下面的例子

    int data = 0; 
    ... 
    do{ 
        ... 
    } while (data != 0); 
    
1

兩個錯誤在這裏:

  • 您的循環將永遠不會執行,因爲在初始運行時,data == 0

    String strdata = JOptionPane.showInputDialog ("Enter the integer The input ends if the value is 0") ; 
    int data = Integer.parseInt(strdata); 
    

    然後,後來在環路(向底部):您可以通過詢問早在後來反對的問題解決此問題

    strdata = JOptionPane.showInputDialog ("Enter the integer The input ends if the value is 0") ; 
    data = Integer.parseInt(strdata); 
    
  • 即使如果你能夠進入循環,你會得到所有正數的數量和沒有負數的數量。原因如下:data > 00 < data是等效的布爾語句。

    你什麼意思大概是這樣的:

    if (data > 0) { 
        positive++; 
    } else if (data < 0) { 
        negative++; 
    } 
    
1

正如其他人所說,初始化data到是一個問題,但請參閱下面我的意見存在。

你要碰到的另一個問題是你有count做得太多。

在0假設count啓動和用戶輸入3

if (data < 0 || data > 0){ 
    count++; 

你在一個現在已經時,你進入

count = count + data; 
    count++; 

和計數上升到4,則++最多爲5.

即使您輸入了0,仍然會在b之前更改計數重複循環。

嘗試類似Orel提到的一個總和詮釋,只是跟蹤總和。請只計數來算,只有計數++在最底層,徹底清除

if (data < 0 || data > 0){ 
     count++; 

相反,嘗試這樣的事情爲0檢查,以防止整個事情擰:

while(true){ 
    String strdata = JOptionPane.showInputDialog ("Enter the integer The input ends if the value is 0") ; 
    data = Integer.parseInt(strdata); 
    if (data == 0) 
      break; 
... 
} 

你可以在這裏使用do/while。這將允許您將數據初始化爲您喜歡的任何(或者什麼都不)。另一件事,確保他們輸入的值不爲零並且是一個整數,當提示輸入數字時,教授肯定會輸入「sleirjwlfie」,因爲假設用戶會打破您的程序是最佳做法。因此,一些僞代碼,如:

while(true){ 
// get value from user 
// if value is null OR value is not an int 
    continue; 
} 

我會讓你理清如何到那裏,以不殺的學習經驗,但我知道,那種事咬了我校不止一次。

祝你好運!