JBTALKS.CC
标题:
JAVA新人求救!!!
[打印本页]
作者:
jayte2168
时间:
2014-10-19 06:55 PM
标题:
JAVA新人求救!!!
我是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;
}
}
}
}
复制代码
欢迎光临 JBTALKS.CC (https://www.jbtalks.cc/)
Powered by Discuz! X2.5