2016-03-06 54 views
-3

我知道這段代碼做了我需要做的工作,但我不禁感到有更多...精煉的方式來獲得相同的結果。請注意,最後幾個輸出僅應顯示如果已經有至少1號(多個)是大於或小於0他們是寫這段代碼的更好方法嗎?

//This program will take an unspecified number of 
//integers, determine how many of those integers are 
//positive and how many are negative, and finally 
//compute the total and average of the integers. 


import java.util.Scanner; 

public class exampleWork { 

public static void main(String[] args) { 
    //create a scanner 
    Scanner input = new Scanner(System.in); 

    //create a variable to hold the positive integers 
    int pos = 0; 

    //create a variable to hold the negative integers 
    int neg = 0; 

    //create a variable to hold the total number of entries 
    int total = 0; 

    //create a variable to hold the average 
    float avg = 0; 

    //create a counter variable 
    int count = 0; 

    //prompt user to enter integer 
    System.out.println("Enter an integer, the input ends if it is 0: "); 
    int usrInput = input.nextInt(); 

     //determine if the number is positive, negative, 
     //or zero, and either place in relative variables, 
     //or end the loop. 
    if (usrInput == 0) 
     System.out.print("Only zero was entered"); 

    while (usrInput != 0) { 

      if (usrInput > 0){ 
      pos++; 
      total += usrInput; 
      count++; 
      System.out.println("Enter an integer, the input ends if it is 0: "); 
      usrInput = input.nextInt(); 


      } if (usrInput < 0){ 
      neg++; 
      total += usrInput; 
      count++; 
      System.out.println("Enter an integer, the input ends if it is 0: "); 
      usrInput = input.nextInt(); 


      } 

     } 
    if (count > 0){ 
    avg = (total/count); 
    System.out.println("The number of positives is " + pos); 
    System.out.println("The number of negatives is " + neg); 
    System.out.println("The total is " + total); 
    System.out.println("The average is " + avg); 
    } 

}

}

+0

是。您可以... –

+0

忽略關於「創建...」的評論,他們沒有增加任何價值。不要描述你做了什麼,這是代碼應該告訴你的。描述你爲什麼這樣做。 –

+0

我主要是以這種方式製作筆記,因爲這是一個班級作業,但它是一個很好的提示,可以知道如何使用筆記。謝謝。我沒有意識到這一點。 – AceFactor

回答

0

您複製的兩個o內部代碼f你的主要ifs。這部分將它們提取出來並清理一些。

while (usrInput != 0) { 
    if (usrInput > 0) 
     pos++; 
    else 
     neg++; 

    total += usrInput; 
    count++; 
    System.out.println("Enter an integer, the input ends if it is 0: "); 
    usrInput = input.nextInt(); 
} 

[編輯,而我只是做一樣XD 6,但現在我做]在一些其他注意事項:

  • 你並不需要保持運行的「數」,因爲它可以在主while循環之後通過添加(pos + neg)來計算。
  • 您正在添加額外的代碼來執行第一次試用。
  • 你正在平均2個整數,這是一個整數,而不是一個浮點數。
  • 你不需要明確地存儲平均值,因爲它只被使用一次。

這是減輕

import java.util.Scanner; 

public class exampleWork { 
    public static void main(String[] args) { 
     //create a scanner 
     Scanner input = new Scanner(System.in); 

     //create a variable to hold the positive integers 
     int pos = 0; 

     //create a variable to hold the negative integers 
     int neg = 0; 

     //create a variable to hold the total number of entries 
     int total = 0; 

     while(true) { 
      System.out.println("Enter an integer, the input ends if it is 0: "); 
      usrInput = input.nextInt(); 
      if(usrInput==0) 
       break; 

      if (usrInput > 0) 
       pos++; 
      else 
       neg++; 

      total += usrInput; 
     } 

     int count=pos+neg; 
     if (count > 0){ 
      System.out.println("The number of positives is " + pos); 
      System.out.println("The number of negatives is " + neg); 
      System.out.println("The total is " + total); 
      System.out.println("The average is " + ((float)total/count)); //Not sure if this typecast is correct, it's been a long time since i have worked in java 
     } 
    } 
} 
+0

謝謝。這似乎是關於我的代碼的常見答案。我想說我在編寫代碼的時候很早就試過這樣做,但當我嘗試在「when」中添加「else」語句時,我得到了某種類型的錯誤* eclipse *,但我不記得爲什麼。如果我沒有運行計數,我將如何獲得最後幾條語句來執行? – AceFactor

+0

如果你有興趣,我爲其他問題編輯了一堆。如果應該回答你剛剛問過的問題。 – Dakusan

0

注意,兩大部分if語句是相同的,因此它們可以在if。外面被提取,而不是重複:

while (usrInput != 0) { 
    total += usrInput; 
    count++; 

    if (usrInput > 0) { 
     pos++; 
    } else { 
     neg++ 
    } 

    System.out.println("Enter an integer, the input ends if it is 0: "); 
    usrInput = input.nextInt(); 
} 
+1

我明白了!我知道這兩個是多餘的,但我想不出編譯它們的方式。謝謝。 – AceFactor