四号码程序:分组号码

时间:2013-10-09 15:57:42

标签: java

我创建了一个程序,可以输入四个必须具有特定要求的数字:
- 必须是4或6的倍数 -if大于500,必须是10的倍数

程序取四个数字并给出它们的总和,它们的平均值,显示四个数字中最小的一个,最后是四个数字中最大的一个。

现在我想尝试显示每个号码所属的组。如果你想知道我想要这些小组:
如果数字介于0到99之间,则为Tens 如果数字在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
请不要提出整个更正的代码!我想自己修理并学习!

2 个答案:

答案 0 :(得分:0)

您的教授计划将适用于您的所有号码。您需要做的就是调用该方法4次。每个号码一次。你需要做的是将他的代码放在自己的方法中。 (记住,如果他把它作为指导给你,那么他可能会希望你自己写下逻辑)。

假设你把他的代码放在一个看似如下的方法中:

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

然后,接下来,您需要做的就是将已加载的每个值传递到该方法中。例如:displayNumberGroup(randomNumber1);。这将计算该数字的分组。我会把这些东西放到上下文中,因为你已经要求不要用勺子给答案了(这是非常值得称道的!)。

答案 1 :(得分:0)

您可以将它们变成数组,而不是使用randomNumber1,randomNumber2等。通过这样做,您可以在给定的开关上放置一个for循环并运行它4次,这样你的所有数字都有组。如果你需要我用一个简单的例子解释这个问题的任何部分我会,但我知道你想自己做大部分事情,所以如果你有任何问题请告诉我。