2013-10-30 22 views
-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); 
    } 


} 
+1

不工作爲什麼?結果是不是你期望或有錯誤?如果有錯誤顯示我們的堆棧,否則如果結果不正確告訴我們你有什麼,你想要什麼 –

+0

該程序應該做什麼?你能給我們更具體的描述嗎? –

+0

你是什麼意思「不起作用」?你會得到什麼錯誤? –

回答

0

你打電話MultiplyingAddingDividing之前,您不會要求用戶輸入A和B。將值初始化爲零,這樣就不會得到非常有趣的結果。

作爲一種風格方面的說明,您應該重新命名方法以小寫字母開頭,以更好地遵循慣例。

+0

但我已經寫了這樣的其他程序,他們都似乎工作。我已經嘗試了一切。該程序應該詢問用戶想要做什麼(+,*或/),然後用戶給程序2個數字,程序會給出結果。三個程序完全獨立工作,但是當它們結合在一起時......這是一團糟。 –

+0

@YeamFox - 如果你認爲自己完美的工作,你沒有正確地測試它們。我可以看到一些錯誤......你不提一些*絕對殘酷的*編程風格! –

+0

@StephenC這是我的第三個程序...我甚至不知道我在做什麼lol –

0

您正在調用您選擇的方法在A和B之前添加,乘法和除法已被設置爲任何值。我建議在嘗試對它們進行操作之前先要求A和B.

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作爲結果。

+0

+1我認爲這是家庭作業;) –

+0

@PeterLawrey是...也許是。但我希望我只是把他推向了正確的方向,並沒有完全解決。 –

+0

是的,這是家庭作業,我的老師說只使用引入C和D以及+1和-1的方法來寫這些操作......這很難,我必須說!!!!我別無選擇...:(( –

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; 
    } 
相關問題