想要将三个程序放在一个,并询问用户他想要继续

时间:2013-10-30 09:38:12

标签: java

这是我写的三个程序之一。但它不起作用......你能帮帮我吗?我一直在寻找互联网上的帮助,但没有!问题是,我不能使用任何先进的东西 - 所以只使用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 个答案:

答案 0 :(得分: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作为结果。

答案 1 :(得分:0)

在致电MultiplyingAddingDividing之前,您不要让用户输入A和B.将值初始化为零,这样就不会得到非常有趣的结果。

作为一个风格侧面注释,您应该将方法重命名为以小写字母开头,以便更好地遵循惯例。

答案 2 :(得分:0)

在A和B设置为任何内容之前,您正在调用您选择的方法“添加,乘法和除法”。我建议在尝试对它们执行操作之前先询问A和B.

答案 3 :(得分: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;
    }