Kennan Wang 2016-12-05 01:32 采纳率: 0%
浏览 1193
已采纳

正整数分解质因数的程序

用这个程序感觉没错啊,输入数据后没反应怎么回事?
public class Decomposition {
public static void main(String[] args) {
String str="";
int j;
System.out.println("请输入一个正整数:");
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
int k=sc.nextInt();
for(j=2;j<=i;j++){
if(i%j==0){
str=str+j+"*";
i=i/j;
j--;
}
}
str=str+j;
sc.close();
System.out.println("分解结果是:"+k+"="+str);
}

}

  • 写回答

4条回答 默认 最新

  • 圣-雄霸天下 2016-12-05 02:05
    关注
     int k = sc.nextInt();
     改成
     int k=i;
    
     for循环中的内容改成:
     if(i%j == 0){
         str += j;
         if(j<i){
            str += "*";
         }
         i = i/j;
         j--;
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?