2015-10-13 55 views
-11
public static void main(String[] args) 
{ 
    Scanner x=new Scanner(System.in); 
    int n=0;int g=0; int term=0;int temp=0; 
    int sum=0; int factor=1; 
    System.out.print("Input N:"); 

    n=x.nextInt(); 
    g=n; 
    if (n<=0) 
    { 
     System.out.println("Please enter a positive integer"); 
     System.exit(0); 
    } 

    if (n>0) 
    { 
     System.out.print("The factors are:"); 

     while (factor<n) 
     { 
      if (n%factor==0) 
      { 
       System.out.print(factor+","); 
      } 
      factor++; 
     } 
    } 
} 

如果我輸入數字8,因素是1,2和4.我試圖實現的是添加8和1,2的因子,這將是結果爲7.添加一個數字的因子

+0

問題是怎麼回事? :) –

+3

看起來像做得很差的作業給我。 – duffymo

回答

0
import java.util.Scanner; 

public class Demo { 
public static void main(String[] args) 
{ 
    Scanner x=new Scanner(System.in); 
    int n=0;int g=0; int term=0;int temp=0; 
    int sum=0; int factor=1; 
    System.out.print("Input N:"); 

    n=x.nextInt(); 
    g=n; 
    int number = 0; 
    if (n<=0) 
    { 
     System.out.println("Please enter a positive integer"); 
     System.exit(0); 
    } 

    if (n>0) 
    { 
     System.out.print("The factors are:"); 

     while (factor<n) 
     { 
      if (n%factor==0) 
      { 
       System.out.println(factor+","); 
       number+=factor; 
      } 
      factor++; 
     } 
    } 
    System.out.println("Sum = "+number); 
} 
} 
+0

非常感謝, –

+0

@Nic Gon:如果它是正確的,你接受然後PLZ標記爲接受或投票 –

+0

我是新來的,我已經按了上按鈕。再次感謝。 –