2017-05-08 12 views
0

私は大学でJavaについて学んでいます。私はこのメソッドを印刷しようとしていますThe product of non-negatives = 21The product of negatives = 10しかし、私は21と10の代わりに両方とも0になっています。私は1つの方法を使って両方の計算を完了することができます。なぜそれは0に等しいですか?ありがとう。1つのJavaメソッドで負の整数と正の整数の積を求めます。回答は印刷されています0

public class Numbers { 

    public static void main(String[] args) { 
     int num1 = -2, num2 = 3, num3 = 7, num4 = -5; 

     findPosNegProd(num1, num2, num3, num4); 
    } 
public static void findPosNegProd(int n1, int n2, int n3, int n4){ 



    int positives = 0; 
    int negatives = 0; 

    if(n1 > 0) positives *= n1; 
    if(n2 > 0) positives *= n2; 
    if(n2 > 0) positives *= n3; 
    if(n3 > 0) positives *= n3; 

    { 
     if(n1 < 0) negatives *= n1; 
    if(n2 < 0) negatives *= n2; 
    if(n2 < 0) negatives *= n3; 
    if(n3 < 0) negatives *= n3; 
    } 

    System.out.println("The product of non-negatives = " + positives); 
    System.out.println("The product of negatives = " + negatives); 
} 
} 
+4

はあなたが0に初期化し、あなたと掛けているので、それ –

+1

に乗じてきています結果はゼロでなければなりません。1で初期化しようとします。 –

+0

0の代わりに1を使ってポジティブとネガティブを0に初期化します。 – mhasan

答えて

0

が1の値を開始すると、変数positivesnegativesを初期化していない0をしても使用される変数を修正するとき、あなたのpositives変数が1が表示されます。

public static void findPosNegProd(int n1, int n2, int n3, int n4){ 

     int positives = 1; 
     int negatives = 1; 

     if(n1 > 0) positives *= n1; 
     if(n2 > 0) positives *= n2; 
     if(n3 > 0) positives *= n3; 
     if(n4 > 0) positives *= n4; 

     { 
      if(n1 < 0) negatives *= n1; 
      if(n2 < 0) negatives *= n2; 
      if(n3 < 0) negatives *= n3; 
      if(n4 < 0) negatives *= n4; 
     } 

     System.out.println("The product of non-negatives = " + positives); 
     System.out.println("The product of negatives = " + negatives); 
    } 

出力:

非ネガの積= 21ネガの積=あなたはこのためにライン

if(n1 > 0) positives *= n1; 

の0を取得している10

0
int positives = 0; 
int negatives = 0; 

の両方が常に0を与えるこのを掛ける0

に初期化されます。

は、あなたがその常にゼロので、0で乗算され、あなたがpositivesnegativesが0

EDITに等しいかどうかを確認するために検証を追加する必要があります。1.

int positives = 1; 
int negatives = 1; 
0

に設定します。場合あなたが受け取る数字は固定されているので、変数を1に初期化するのに問題はありません。もしあなたがこの関数を使うのであれば、それは間違った結果を示すかもしれません。

すなわち:すべての数値が負の場合、そのは本当に0

0

上記の行は、

if(n1 > 0) 
{ 
    positives = positives * n1; 
} 

あなたがinitilizedしておりますので陽性& negaitives 0に、あなたが受け取る結果が0

0
//I believe this code would solve your problem 
public class NumberProduct 
{ 
    public static void main(String[] args) 
    { 
     int num1=-2, num2=3, num3=7, num4=-5; 
     int negNo=1,posNo=1; 

// This code is to sort the products by multiplying positive numbers and also negative numbers and storing them in different variables 
if (num1<0) 
{ 
negNo=num1; 
} 
else 
{ 
posNo=num1; 
} 
if (num2<0) 
{ 
negNo*=num2; 
} 
else 
{ 
posNo*=num2; 
} 
if (num3<0) 
{ 
negNo*=num3; 
} 
else 
{ 
posNo*=num3; 
} 
if (num4<0) 
{ 
negNo*=num4; 
} 
else 
{ 
posNo*=num4; 
} 
    System.out.println("The product of negatives is " + negNo); 
    System.out.println("The product of positives is " + posNo); 
} 

}

0
It can also be solved by initializing your values into an array 
     public static void main(String[] args) 
     { 
//int num1=-2, num2=3, num3=7, num4=-5; 
     int negatives=1, positives=1; 
     int num [] = { -2, 3, 7, -5}; 

     for(int i=0;i<num.length;i++) 
     { 
       if (num[i]<0) 
       { 
        negatives*=num[i]; 
       } 
       else 
       { 
        positives*=num[i]; 
       } 
     } 

     System.out.println("The product of negatives is " + negatives); 
     System.out.println("The product of positives is " + positives); 
     } 
関連する問題