-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

为什么你调用你的方法两次?
也许您可以这样尝试:

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

在调用或之前Multiplying,您不会要求用户输入 A 和B。将值初始化为零时,您不会以这种方式获得非常有趣的结果。AddingDividing

作为文体方面的说明,您应该将方法重命名为以小写字母开头,以更好地遵循通常的约定。

于 2013-10-30T09:42:19.657 回答
0

在将 A 和 B 设置为任何值之前,您正在调用您选择的方法加法、乘法和除法。我建议在尝试对它们执行操作之前询问 A 和 B。

于 2013-10-30T09:42:48.393 回答
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 回答