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的编排不对? 求高手指点 谢谢!
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 i;
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(z =0; z < size; z++)
{
System.out.println("Number "+(z+1)+"?");
int num = input.nextInt();
if( y >= 6 && y <= 1000)
{
System.out.println("Factors are:");
for(i = 1; i < num; i++)
{
if(num % i == 0)
{
x = x + i;
System.out.println(i);
}
}
if(x==num)
{
System.out.println("It is a perfect number.");
}
if(x>num)
{
System.out.println("It is an eycessive number.");
}
if(x<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