|登录 |注册

查看: 811|回复: 3
打印 上一主题 下一主题

JAVA PROBLEM

[复制链接]
jayte2168
2014-10-15 09:36 AM
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.
  1. import java.util.*;

  2. public class Number {

  3.         public static void main(String[] args)
  4.         {
  5.                 Scanner in = new Scanner(System.in);
  6.                
  7.                 while (true)
  8.                 {
  9.                         System.out.println("How many number do you want to test? ");
  10.                        
  11.                         try
  12.                         {
  13.                             int size = in.nextInt();
  14.                             System.out.println("Enter a number,x which  6 <= x <= 1000");

  15.                             List<Integer> numbers = new ArrayList(0);
  16.                             for (int i=0;i<size;i++) {
  17.                               System.out.println("Number "+(i+1)+"?");
  18.                               int input = in.nextInt();
  19.                               numbers.add(input);
  20.                             }

  21.                             for (Integer num : numbers) {
  22.                               printResult(sumOfDivisors(getFactors(num)), num);
  23.                             }
  24.                                 int input = in.nextInt();
  25.                                 if (inputError(input))
  26.                                 {
  27.                                         System.out.println("Input Range Error!");
  28.                                         System.out.println();
  29.                                         continue;
  30.                                 }
  31.                                 List<Integer> factors = getFactors(input);
  32.                                 printFactors(factors);
  33.                                 int sum = sumOfDivisors(factors) - input;
  34.                                 System.out.printf("Sum of divisors: %d", sum);
  35.                                 System.out.println();
  36.                                 printResult(sum, input);
  37.                                 System.out.println();
  38.                                 
  39.                         }
  40.                         catch (InputMismatchException e)
  41.                         {
  42.                                 in.next(); // prevent repeat reading
  43.                                 System.out.println("Input Type Error!");
  44.                                 System.out.println();
  45.                                 continue;
  46.                         }
  47.                 }
  48.                
  49.                
  50.         }
  51.         
  52.         private static boolean inputError(int input)
  53.         {
  54.                 if (input < 6 || input > 1000) return true;
  55.                 return false;
  56.         }
  57.         
  58.         private static List<Integer> getFactors(int n)
  59.         {
  60.                 List<Integer> factors = new ArrayList<Integer>(0);
  61.                 factors.add(1);
  62.                 factors.add(n);
  63.                 for (int i=2; i < n/2; i++)
  64.                 {
  65.                         if (n % i == 0)
  66.                         {
  67.                                 if (factors.indexOf(i) < 0)
  68.                                         factors.add(i);
  69.                                 if (factors.indexOf(n/i) < 0)
  70.                                         factors.add(n/i);
  71.                         }
  72.                 }
  73.                 factors = sortFactors(factors);
  74.                 return factors;
  75.         }
  76.         
  77.         private static void printFactors(List<Integer> factors)
  78.         {
  79.                 System.out.print("Factors: ");
  80.                 for (Integer factor : factors)
  81.                 {
  82.                         System.out.print(factor + " ");
  83.                 }
  84.                 System.out.println("");
  85.         }
  86.         
  87.         private static int sumOfDivisors(List<Integer> factors)
  88.         {
  89.                 int sum = 0;
  90.                 for (Integer factor : factors)
  91.                         sum += factor;
  92.                 return sum;
  93.         }
  94.         
  95.         private static void printResult(int sum, int input)
  96.         {
  97.                 if (sum - input == input)
  98.                 {
  99.                         System.out.printf("%d is a Perfect Number!\n", input);
  100.                 }
  101.                 else if (sum - input > input)
  102.                 {
  103.                         System.out.printf("%d is a Excessive Number!\n", input);
  104.                 }
  105.                 else
  106.                 {
  107.                         System.out.printf("%d is a Defective Number!\n", input);
  108.                 }
  109.         }
  110.         
  111.         private static List<Integer> sortFactors(List<Integer> factors)
  112.         {
  113.             Comparator c = new Comparator()
  114.             {
  115.                     public int compare(Object arg0, Object arg1)
  116.                     {
  117.                             return ((Integer)arg0).compareTo((Integer)arg1);
  118.                     }
  119.             };
  120.             Collections.sort(factors, c);
  121.             return factors;
  122.     }

  123.         
  124. }
复制代码
03-342
2014-10-15 10:41 AM
我也想知道答案~
tonywonghs
2014-10-15 12:46 PM
楼主小心被关帖,因为帖中一句中文都没有,你太懒惰了~~
jayte2168
2014-10-15 02:50 PM
tonywonghs 发表于 2014-10-15 12:46 PM
楼主小心被关帖,因为帖中一句中文都没有,你太懒惰了~~

I'm using college pc, so i can't type chinese ...... =(
您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2026-1-1 10:29 AM , Processed in 0.093356 second(s), 25 queries .

Powered by Discuz! X2.5 © 2001-2012 Comsenz Inc.

本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved

Dedicated Server powered by iCore Technology Sdn. Bhd.

合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部