这是我写的三个程序,合二为一。但它不起作用......你能帮帮我吗?我一直在互联网上寻求帮助,但没有!问题是,我不能使用任何高级的东西——所以只使用 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);
}
}