JBTALKS.CC

标题: java问题 [打印本页]

作者: jayte2168    时间: 2014-10-18 05:55 PM
标题: java问题
本帖最后由 jayte2168 于 2014-10-18 05:58 PM 编辑

我是java新人 在写着一个计算程序,用来测试perfect num, excessive num和decessive num

在program里 我必须拿到的input是:
1) amount of number to be tested (how many times to loop)
2) 一个整数

output则是:
1) list of divisor of the input
2) sum of the divisors
3) message state the result

我遇到的问题是 我已经成功做到input的部分了 可是不能得到那些号码的所有output和result
请问我是少了什么呢? 还是code的编排不对? 求高手指点 谢谢!
  1. import java.util.*;
  2. public class Numbers {

  3.         public static void main(String[]args){
  4.         
  5.         Scanner input =new Scanner(System.in);
  6.         int x=0;
  7.         int y=0;
  8.         int z=0;
  9.         int i;
  10.         int sum=0;
  11.         
  12.         while (true)
  13.         {
  14.                 System.out.println("How many numbers will you enter?");
  15.                
  16.                 try
  17.                 {
  18.                        int size = input.nextInt();
  19.                        System.out.println("Enter any number.");
  20.                       
  21.                         for(z =0; z < size; z++)
  22.                         {
  23.                                 System.out.println("Number "+(z+1)+"?");
  24.                                int num = input.nextInt();
  25.                                
  26.                         if( y >= 6 && y <= 1000)
  27.                     {
  28.                             System.out.println("Factors are:");
  29.                         for(i = 1; i < num; i++)
  30.                         {
  31.                                 if(num % i == 0)
  32.                                 {
  33.                                         x = x + i;
  34.                                         System.out.println(i);
  35.                                 }
  36.                         }
  37.                        if(x==num)
  38.                        {
  39.                             System.out.println("It is a perfect number.");
  40.                        }
  41.                        if(x>num)
  42.                        {
  43.                                        System.out.println("It is an eycessive number.");
  44.                        }
  45.                        if(x<num)
  46.                        {
  47.                                        System.out.println("It is a defective number.");
  48.                        }
  49.                     }
  50.                    else
  51.                    {
  52.                            System.out.println("It is not a valid number. Please reenter a number that is in the range of 6 to 1000.");
  53.                            continue;
  54.                    }
  55.                         }
  56.                 }
  57.                 catch(InputMismatchException e)
  58.                 {
  59.                         input.next();
  60.                 System.out.println("Input Type Error! Please enter an integer.");
  61.                 System.out.println();
  62.                 continue;
  63.                 }
  64.         }
  65.         }
  66. }
复制代码





欢迎光临 JBTALKS.CC (https://www.jbtalks.cc/) Powered by Discuz! X2.5