我創建了一個程序,使您可以鍵入四個號碼必須有特定的要求:
- 必須是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();
}
}
這裏是我的代碼成績單:
我的教授給我一組程序(如下圖)的這個樣本
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();
}
}
下面是他的代碼抄本:
我知道他的計劃是要求用戶在鍵入一個號和程序組吧。我想知道如何將這樣的東西放入我的程序中。我很困惑,因爲我有四個數字必須分組,我不確定是否需要爲我的四個數字(randomNumber1,randomNumber2,randomNumber3,randomNumber4
)重複與我的教授計劃類似的內容請不要設置整個更正的代碼!我想自己修復它並學習!
看到你教授的代碼後,我認爲他不應該教某人java/algorithms /編碼或他教的任何東西 –
我很遺憾地同意你 – Morgan