2013-10-30 1 views
-1

여기에 제가 작성한 세 가지 프로그램이 있습니다. 하지만 작동하지 않습니다 ... 도와 주시겠습니까? 나는 인터넷을 통해 도움을 찾고 있었지만 아무 것도하지 않았다! 문제는 고급 기능을 사용할 수 없다는 것입니다. if 및 while을 사용하면 ... 왜 작동하지 않습니까? 여기에 프로그램은 : 당신이 Multiplying, Adding 또는 Dividing를 호출하기 전에3 개의 프로그램을 하나씩 넣고 싶은 사람에게 묻습니다.

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

당신은 입력 A와 B로 사용자를 요구하지 않습니다. 값을 0으로 초기화하면이 방법으로는 매우 흥미로운 결과를 얻을 수 없습니다.

문체 측면 참고로, 일반적인 규칙을 잘 따르려면 소문자로 시작하는 메서드의 이름을 변경해야합니다.

+0

그러나 나는 다른 프로그램을 썼다. 나는 모든 것을 시도했다. 프로그램은 사용자가 원하는 것을 묻기로되어 있고 (+, * 또는 /) 사용자가 프로그램에 2 개의 숫자를 주면 프로그램은 그 결과를 내 보냅니다. 세 가지 프로그램은 모두 독자적으로 작동하지만 모두 함께 결합하면 ... 엉망입니다. –

+0

@YeamFox - 그들이 완벽하게 작동한다고 생각한다면 제대로 테스트하지 않은 것입니다. 나는 많은 버그를 볼 수있다. * 절대적으로 흉포 한 * 프로그래밍 스타일을 언급하지는 않는다! –

+0

@StephenC 이것은 내 세 번째 프로그램입니다 ... 나는 심지어 내가하고있는 일을 잘 모르겠다 –

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 yes ...아마 그렇 겠지. 그러나 나는 단지 그를 올바른 방향으로 밀어 넣고 그것을 완전히 풀지 않았 으면 좋겠다. –

+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; 
    } 
관련 문제