- 分享
- 0
- 人气
- 0
- 主题
- 9
- 帖子
- 28
- UID
- 348244
- 积分
- 303
- 阅读权限
- 15
- 注册时间
- 2010-9-7
- 最后登录
- 2016-7-21
- 在线时间
- 159 小时
|
The below coding is incomplete and in a messy order, so i'm finding someone to arrange them for me.
This program is to test the perfect number, excessive number and decessive number.
In the program:
1)prompt the user for input
2)inputs from user:
1) amount of number to be tested (how many times to loop)
For example,
Please enter the numbers of value to be tested.
4
Please enter the 1st number.
60
(then the result)
Please enter the 2nd number.
80
(then the result)
Please enter the 3rd number.
100
(then the result)
Please enter the 4st number.
500
(then the result)
2) the whole number (between 6-1000)
3)check that the values entered fall within the valid ranges, if not, show warning and prompt to re-enter
4)after input, begin to loop
5)output:
1) list of divisor of the input
2) sum of the divisors
3) message state the result
The output only display the proper divisor.- import java.util.*;
- public class Number {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
-
- while (true)
- {
- System.out.println("How many number do you want to test? ");
-
- try
- {
- int size = in.nextInt();
- System.out.println("Enter a number,x which 6 <= x <= 1000");
- List<Integer> numbers = new ArrayList(0);
- for (int i=0;i<size;i++) {
- System.out.println("Number "+(i+1)+"?");
- int input = in.nextInt();
- numbers.add(input);
- }
- for (Integer num : numbers) {
- printResult(sumOfDivisors(getFactors(num)), num);
- }
- int input = in.nextInt();
- if (inputError(input))
- {
- System.out.println("Input Range Error!");
- System.out.println();
- continue;
- }
- List<Integer> factors = getFactors(input);
- printFactors(factors);
- int sum = sumOfDivisors(factors) - input;
- System.out.printf("Sum of divisors: %d", sum);
- System.out.println();
- printResult(sum, input);
- System.out.println();
-
- }
- catch (InputMismatchException e)
- {
- in.next(); // prevent repeat reading
- System.out.println("Input Type Error!");
- System.out.println();
- continue;
- }
- }
-
-
- }
-
- private static boolean inputError(int input)
- {
- if (input < 6 || input > 1000) return true;
- return false;
- }
-
- private static List<Integer> getFactors(int n)
- {
- List<Integer> factors = new ArrayList<Integer>(0);
- factors.add(1);
- factors.add(n);
- for (int i=2; i < n/2; i++)
- {
- if (n % i == 0)
- {
- if (factors.indexOf(i) < 0)
- factors.add(i);
- if (factors.indexOf(n/i) < 0)
- factors.add(n/i);
- }
- }
- factors = sortFactors(factors);
- return factors;
- }
-
- private static void printFactors(List<Integer> factors)
- {
- System.out.print("Factors: ");
- for (Integer factor : factors)
- {
- System.out.print(factor + " ");
- }
- System.out.println("");
- }
-
- private static int sumOfDivisors(List<Integer> factors)
- {
- int sum = 0;
- for (Integer factor : factors)
- sum += factor;
- return sum;
- }
-
- private static void printResult(int sum, int input)
- {
- if (sum - input == input)
- {
- System.out.printf("%d is a Perfect Number!\n", input);
- }
- else if (sum - input > input)
- {
- System.out.printf("%d is a Excessive Number!\n", input);
- }
- else
- {
- System.out.printf("%d is a Defective Number!\n", input);
- }
- }
-
- private static List<Integer> sortFactors(List<Integer> factors)
- {
- Comparator c = new Comparator()
- {
- public int compare(Object arg0, Object arg1)
- {
- return ((Integer)arg0).compareTo((Integer)arg1);
- }
- };
- Collections.sort(factors, c);
- return factors;
- }
-
- }
复制代码 |
|