-1

これが、私が書いた 3 つのプログラムを 1 つにまとめたものです。しかし、うまくいきません...助けていただけませんか?私は助けを求めてインターネット中を探していましたが、何もありません!問題は、高度なものを使用できないことです。if と while を使用するだけです...なぜこれが機能しないのですか? ここにプログラムがあります:

   import java.util.Scanner;

public class Threeinon {

public static void Adding (int A, int B)
{   
    int C, D;

    if (A>0 && B>0) 
       {
       C=A;
       D=B;
       while (D!=0) {
            D=D-1;
            C=C+1 ;
       }
       System.out.println("The summation is: " + C);
       }



       if (A>0 && B<0)
       {
           C=A;
           D=B;

           while (D!=0)
           {
               C=C-1;
               D=D+1;
           }
           System.out.println("The summation is: " + C);
       }


       if (A<0 && B>0)
       {
           C=A;
           D=B;

           while (D!=0) {
               C=C+1;
               D=D-1;

           }
           System.out.println("The summation is: " + C);
       }

       if (A<0 && B<0)
       {
           C=A;
           D=B;

           while (D !=0)
           {
               C=C-1;
               D=D+1;

           }
           System.out.println("The summation is: " + C);
       }
}

public static void Multiplying (int A, int B)
{
    int C, D;

     if (A>0 && B>0 )
       {
           C=0;
           D=A;
           while (D!=0){
                D=D-1 ;
                C=B+C;
                System.out.println("The result is:" +C);
                System.out.println("The second result is true.");
           }
       }
       if (A==0)
       { 
           System.out.println("The result is 0");      
       }
       { 
       if (B==0)
       {
           System.out.println("The result is 0");
       }

       if (A<0 )
       {
           C=0;
           D=B;
           while (D != 0) {
               C=C+A;
               D=D-1;
           }
           System.out.println("the result is:"+C);
       }
       if (B<0 )
       {
           C=0;
           D=A;
           while (D != 0){
               C=C+B;
               D=D-1;
           }
           System.out.println("the result is:"+C);
       }

       if ( A<0 && B<0 )
       {
           C=0;
           D=B;
           while (D!=0){
               C=A+C;
               D=D+1;
               System.out.println("the result is:" +C);
               System.out.println("Only the first result is correct which is positive");
           }
       }

       }
}

public static void Dividing (int A, int B)
{
    int C, D;
    if (A>0 && B>0){
           C=0;
           D=A;
           while (D>=B){
                D=D-B ;
                C=C+1;
           }
           System.out.println("the result is:" + C);

           }

           if (A<0 && B>0)
           {
               C=0;
               D=-A;

               while (D>=B)
               {
                   D=D-B;
                   C=C-1;

               }
               System.out.println("the result is:" +C);
           }

           if (A>0 && B<0 )
           {
               C=0;
               D=A;

               while (D !=0 )
               {
                   C=C-1;
                   D=D+B;

               }
               System.out.println("the result is:" +C);
           }

           if (A<0 && B<0 )
           {
               C=0;
               D=-A;

               while (D != 0)
               {
                   D=D+B;
                   C=C+1;
               }
               System.out.println("the result is:" +C);
           }


}


public static void main(String[] args){

    int A=0, B=0;
    int n;

    Scanner scan = new Scanner(System.in);
    System.out.println(" Enter 1 for multiplying");
    System.out.println(" Enter 2 for adding");
    System.out.println(" Enter 3 for dividing");
    n= scan.nextInt();

    if ( n==1 )
    {
        Multiplying (A, B) ;
    }
    else {
        if ( n==2 )
    {
        Adding (A, B);
    }
        else {
            if ( n==3 )
    {
        Dividing (A, B);
    }
        }

    }

       System.out.println("Ignore the two 0 you see, proceed");
       System.out.println("Enter first number:");
       A = scan.nextInt();
       System.out.println("Enter second number:");
       B = scan.nextInt();

      Adding (A, B);
      Multiplying (A,B);
          Dividing (A, B);
    }


}
4

4 に答える 4

1

メソッドを 2 回呼び出すのはなぜですか?
多分あなたはこの方法で試してみてください:

public static void main(String[] args){

int A=0, B=0;
int n;

Scanner scan = new Scanner(System.in);
System.out.println(" Enter 1 for multiplying");
System.out.println(" Enter 2 for adding");
System.out.println(" Enter 3 for dividing");
n= scan.nextInt();
System.out.println("Enter first number:");
A = scan.nextInt();
System.out.println("Enter second number:");
B = scan.nextInt();

if ( n==1 )
{
    Multiplying (A, B) ;
}
else {
    if ( n==2 )
    {
        Adding (A, B);
    }
    else {
        if ( n==3 )
        {
            Dividing (A, B);
        }
    }

}
}

ところで、メソッドを少し短くした方がずっと簡単だと思いませんか? それともこれがあなたの目的ですか?
例えば:

public static void Adding (int A, int B)
{   
       System.out.println("The summation is: " + (A + B));
}
...

数学演算子は、これまでと同じように使用できます。+, -, *, /. ただし、分割に注意して/ください。結果は である必要はないintので、結果をより適切に使用doubleしてください。

于 2013-10-30T09:57:02.603 に答える
0
int A=0, B=0;
    int n;

    Scanner scan = new Scanner(System.in);
    System.out.println(" Enter 1 for multiplying");
    System.out.println(" Enter 2 for adding");
    System.out.println(" Enter 3 for dividing");
    n= scan.nextInt();
 System.out.println("Enter first number:");
       A = scan.nextInt();
       System.out.println("Enter second number:");
       B = scan.nextInt();
    switch(n){
      case 1:Multiplying(A,B);
      break;
      case 2:Adding(A,B);
      break;
      case 3:Dividing(A,B);
      break;
    }
于 2013-10-30T10:15:01.770 に答える
0

またはを呼び出す前Multiplyingに、ユーザーに A と B の入力を求めません。ゼロで初期化された値では、この方法ではあまり興味深い結果は得られません。AddingDividing

文体上の補足事項として、通常の慣例によりよく従うために、小文字で始まるメソッドの名前を変更する必要があります。

于 2013-10-30T09:42:19.657 に答える
0

A と B が何かに設定される前に、Adding、Multiplying、Dividing のメソッドの選択を呼び出しています。A と B に対して操作を実行する前に、A と B について尋ねることをお勧めします。

于 2013-10-30T09:42:48.393 に答える