- 分享
- 0
- 人气
- 0
- 主题
- 9
- 帖子
- 28
- UID
- 348244
- 积分
- 303
- 阅读权限
- 15
- 注册时间
- 2010-9-7
- 最后登录
- 2016-7-21
- 在线时间
- 159 小时
|
我是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的编排不对? 求高手指点 谢谢!
- import java.util.*;
- public class Numbers {
- public static void main(String[]args){
-
- Scanner input =new Scanner(System.in);
- int x=0;
- int y=0;
- int z=0;
- int sum=0;
- while (true)
- {
- System.out.println("How many numbers will you enter?");
-
- try
- {
- int size = input.nextInt();
- System.out.println("Enter any number.");
-
- for(x =0; x < size; x++)
- {
- System.out.println("Number "+(x+1)+"?");
-
- int num = input.nextInt();
- if( num >= 6 && num <= 1000)
- {
- System.out.println("Factors are:");
- for(int i = 1; i < num; i++)
- {
- if(num % i == 0)
- {
- z = z+i;
- System.out.println(i);
- }
- }
- if(z==num)
- {
- System.out.println("It is a perfect number.");
- }
- if(z>num)
- {
- System.out.println("It is an excessive number.");
- }
- if(z<num)
- {
- System.out.println("It is a defective number.");
- }
- }
- else
- {
- System.out.println("It is not a valid number. Please reenter a number that is in the range of 6 to 1000.");
- continue;
- }
- }
- }
- catch(InputMismatchException e)
- {
- input.next();
- System.out.println("Input Type Error! Please enter an integer.");
- System.out.println();
- continue;
- }
- }
- }
- }
复制代码 |
|