下面是我編寫的三個程序。但它不起作用...你能幫我嗎?我一直在尋找互聯網尋求幫助,但沒有任何!事情是,我不能使用任何先進的東西,所以只使用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);
}
}
不工作爲什麼?結果是不是你期望或有錯誤?如果有錯誤顯示我們的堆棧,否則如果結果不正確告訴我們你有什麼,你想要什麼 –
該程序應該做什麼?你能給我們更具體的描述嗎? –
你是什麼意思「不起作用」?你會得到什麼錯誤? –