2013-10-09 67 views
0

我創建了一個程序,使您可以鍵入四個號碼必須有特定的要求:
- 必須是4的倍數或6
- 如果大於500,必須是多的10四個數字方案:分組編號

該程序將四個數字,並給你他們的總和,他們的平均值,顯示你四個數字中最小的,最後是四個數字中最大的。

現在我想嘗試並顯示每個數字所屬的組。我想這些羣體,如果你想知道:
集團數萬,如果數字是集團數百名0到99之間
,如果數字是集團數以千計的100和999 之間
如果數字爲集團其他如1000和999,999之間
數量大於999,999

我的問題是我不知道我應該被安置他們到我的程序(如下圖)

import java.util.Scanner; 

public class FourNumbersProgram { 

private static int readNumber(String message, Scanner in) { 
    System.out.println("Enter a numbers divislbe by 4 or 6. No negatives."); 
    System.out.println("**If greater than 500: must be multiple of 10"); 
    System.out.print(message); 
    while (!in.hasNextInt()) { 
     in.next(); 
     System.out.println("Sorry, couldn't understand you!"); 
     System.out.print(message); 
    } 
    int a = in.nextInt(); 
    return a; 
} 

private static int readNumberToMatchCondition(String message, Scanner in) { 
    int number = 0; 
    do { 
     number = readNumber(message, in); 
     if (number < 500) { 
      if (number % 4 != 0 && number % 6 != 0) { 
       System.out.println(number + " not divisible by 4 or 6"); 
      } else { 
       return number; 
      } 
     } else { 
      if (number % 4 != 0 && number % 6 != 0) { 
       System.out.println(number + " not divisible by 4 or 6"); 
      } else if (number % 10 != 0) { 
       System.out.println(number + " is greater than 500 and not divisible by 10"); 
      } else { 
       return number; 
      } 

     } 
    } while (true); 
} 

public static void main(String[] args) { 
    Scanner in = new Scanner(System.in); 

    int randomNumber1 = readNumberToMatchCondition("Enter first number:", in); 
    int randomNumber2 = readNumberToMatchCondition("Enter second number:", in); 
    int randomNumber3 = readNumberToMatchCondition("Enter third number:", in); 
    int randomNumber4 = readNumberToMatchCondition("Enter fourth number:", in); 


    int sum; // sum of number1, number2, number3, and number4 
    int avg; // average of number1, number2, number3, and number4 
    int largest; // largest number of the four integers 
    int smallest; // smallest number of the four integers 

    sum = (randomNumber1 + randomNumber2 + randomNumber3 + randomNumber4); 

    avg = ((sum)/4); 

     smallest = randomNumber1; 
     smallest = (randomNumber2 < smallest) ? randomNumber2 : smallest; 
     smallest = (randomNumber3 < smallest) ? randomNumber3 : smallest; 
     smallest = (randomNumber4 < smallest) ? randomNumber3 : smallest; 


     largest = randomNumber1; 
     largest = (randomNumber2 > largest) ? randomNumber2 :largest; 
     largest = (randomNumber3 > largest) ? randomNumber3 :largest; 
     largest = (randomNumber4 > largest) ? randomNumber4 :largest; 

    System.out.println(); 
    System.out.println("First number entered: " + randomNumber1); //prints first number entered 
    System.out.println("Second number entered: " + randomNumber2); //prints second number entered 
    System.out.println("Third number entered: " + randomNumber3); //prints third number entered 
    System.out.println("Fourth number entered: " + randomNumber4); //prints fourth number entered 
    System.out.println(); 
    System.out.println("The sum is: " + sum); //prints sum of four numbers 
    System.out.println("The average is: " + avg); //prints average of four numbers 
    System.out.println("The smallest number is: " + smallest); //prints smallest of the four numbers 
    System.out.println("The largest number is: " + largest); //prints largest of the four numbers 
    System.out.println(); 
} 
} 



這裏是我的代碼成績單:
enter image description here


我的教授給我一組程序(如下圖)的這個樣本


import java.util.Scanner; 

public class Prog2 { 

    public static void main(String[] args){ 

     Scanner in = new Scanner(System.in);// read a number 

     System.out.println("Enter a number: "); 
     int number = in.nextInt(); 

     if (number < 0) { 
      System.out.println("Error: Sorry, no negative numbers allowed."); 

      System.exit(0); 
     } 


     int grp1 = (number/100) + 1; 
     int grp2 = (number/1000) + 1; 
     int grp3 = (number/1000000) + 1; 

     switch (grp1){ 
     case 1: // Group Tens 
      System.out.println(number + " belongs to Group Tens"); 

      break; 
     default: 
      switch (grp2) { 
      case 1:// Group Hundreds 
       System.out.println(number + " belongs to Group Hundreds"); 

       break; 
      default: 
       switch (grp3) { 
       case 1:// Group Thousands 
         System.out.println(number + " belongs to Group Thousands"); 

         break; 
       default:// Group Others 
         System.out.println(number + " belongs to Group Others"); 
} 

} 

} 

in.close(); 
} 

} 



下面是他的代碼抄本:
enter image description here


我知道他的計劃是要求用戶在鍵入一個號和程序組吧。我想知道如何將這樣的東西放入我的程序中。我很困惑,因爲我有四個數字必須分組,我不確定是否需要爲我的四個數字(randomNumber1,randomNumber2,randomNumber3,randomNumber4
)重複與我的教授計劃類似的內容請不要設置整個更正的代碼!我想自己修復它並學習!

+4

看到你教授的代碼後,我認爲他不應該教某人java/algorithms /編碼或他教的任何東西 –

+0

我很遺憾地同意你 – Morgan

回答

0

您的教授計劃將適用於您所有的電話號碼,您所需要做的就是撥打方法4次,每次撥打一次號碼。 (請記住,如果他把它給了你作爲指導,那麼他可能會希望你自己寫邏輯)

讓我們假設你把他的代碼放在一個方法上代碼的方法如下所示:

public void displayNumberGroup(int number) 
{ 
    // Displays number group using your prof's code. 
} 

然後,接下來,您需要做的就是將每個已加載的值傳遞給該方法。例如:displayNumberGroup(randomNumber1);。這將計算該號碼的分組。我會把它放在你的背景下,因爲你已經要求不要提供答案(這非常值得稱道!)。

+0

對不起,但我不明白當你說「你需要做的就是調用該方法四次「當你的意思是方法你的意思是我將教授代碼的方法? – Morgan

+0

這是正確的。將你的教授的代碼放入一個方法中,並用四個數字中的每一個來調用該方法。 – christopher

0

而不是做randomNumber1,randomNumber2等等,你可以讓他們成爲一個數組。通過這樣做,你可以在給定的開關上放一個for循環並運行4次,這樣你的所有數字都有組。如果你需要我用一個簡單的例子解釋這個問題的任何部分,但我知道你自己想要做的大部分工作,所以如果你有任何問題,請告訴我。