このコードは、2 つの整数と演算を受け取り、数値を計算することになっています。これはプログラム全体の一部に過ぎず、他のすべては機能しますが、計算は間違っています。全体を実行したときに得られる唯一の正しい出力は、減算です。どうしたの?
public static double calculate(int operand1, int operand2, char operation)
{
if (operation=='^')
{
return (Math.pow(operand1,operand2));
}
else if(operation=='+')
{
return ((operand1)+(operand2));
}
else if(operation=='-')
{
return (operand1-operand2);
}
else if(operation=='*')
{
return (operand1*operand2);
}
else
{
if (operand2==0)
{
System.out.println("Cant divide by zero");
}
return(operand1/operand2);
}
}
これがプログラムのコード全体です
import java.util.Scanner;
public class Calculator
{
public static void main(String[] args)
{
printIntro();
Scanner kb = new Scanner(System.in);
String answer = "yes";
while (answer.equals("yes"))
{
System.out.println("Enter your problem");
String fullExpression=kb.nextLine();
fullExpression=fullExpression.replaceAll("\\s","");
int length=fullExpression.length();
char op1=fullExpression.charAt(0);
char op2=fullExpression.charAt(1);
String operation=fullExpression.substring(2,length);
printEnglish(op1,op2,operation);
System.out.println("Type yes to continue");
Scanner kb1 = new Scanner(System.in);
answer=kb1.nextLine();
};
}
/*this methods gets the operands and the operation and
prints the English version: if we call this method
printEnglish(2,3, plus), then this method will output:
Two plus three = 5 */
public static void printEnglish(char op1, char op2, String operation)
{
String resultOp1CharToString=charToString(op1);
String resultOp2CharToString=charToString(op2);
char resultOperationConversion=operationConversion(operation);
double resultCalculate=calculate(op1, op2, resultOperationConversion);
System.out.print(resultOp1CharToString+ operationConversion(operation) + resultOp2CharToString+ "=" + resultCalculate);
/*1. call the method charToString(op1) to convertlish
word for example ‘1’ to one or ‘2’ to two,….
2. call the method operandConversionToNumber(op1) to
get its numeric value. For example if op1 is ‘1’ then
this method call should return the integer value 1
3. call the method operationConversion(operation) to
convert the operation to a mathematical operation. For
example if you call this method with the string plus
then it will return ‘+’
4. finally call the method calculate to get the result
of the operation.*/
}
/*this method prints the numeric version which is 2 *3
=6*/
//public static boolean printNumeric(char op1, char op2, String operation)
//{
/*String resultCharToString=charToString(op1);
String resultCharToString2=charToString(op2);
int resultOperandToNumber=operandConversionToNumber(op1);
int resultOperandToNumber2=operandConversionToNumber(op2);
char resultOperationConversion=operationConversion(operation);
double resultCalculate=calculate(op1, op2, operation); */
//}
/*this method gets a number as a character and returns
its numeric value as an integer. You must use case
statement for this method*/
public static int operandConversiontoNumber(char operand)
{
int numberOperand=0;
switch(operand)
{
case '0':
numberOperand=0;
break;
case '1':
numberOperand=1;
break;
case '2':
numberOperand=2;
break;
case '3':
numberOperand=3;
break;
case '4':
numberOperand=4;
break;
case '5':
numberOperand=5;
break;
case '6':
numberOperand=6;
break;
case '7':
numberOperand=7;
break;
case '8':
numberOperand=8;
break;
case '9':
numberOperand=9;
break;
}
return numberOperand;
}
/*this method gets the operation as a string and
return the equivalent operation in math. For example
if it receives “plus” the it will return ‘+’ */
public static char operationConversion(String s)
{
char operation=0;
if(s.equals("plus"))
{
operation= '+';
}
else if(s.equals("minus"))
{
operation= '-';
}
else if(s.equals("multiply"))
{
operation= '*';
}
else if(s.equals("divide"))
{
operation= '/';
}
else
{
operation= '^';
}
return operation;
}
/*this method recives two numbers and the operation
and returns the result*/
public static double calculate(int operand1, int operand2, char operation)
{
if (operation=='^')
{
return (Math.pow(operand1,operand2));
}
else if(operation=='+')
{
return ((operand1)+(operand2));
}
else if(operation=='-')
{
return (operand1-operand2);
}
else if(operation=='*')
{
return (operand1*operand2);
}
else
{
if (operand2==0)
{
System.out.println("Cant divide by zero");
}
return(operand1/operand2);
}
}
/*this method converst a number character to its
English word for example if this method receives ‘1’
it will return “one” */
public static String charToString(char num)
{
String englishOperand="one";
switch(num)
{
case '0':
englishOperand= "zero";
break;
case '1':
englishOperand="one";
break;
case '2':
englishOperand="two";
break;
case '3':
englishOperand="three";
break;
case '4':
englishOperand= "four";
break;
case '5':
englishOperand= "five";
break;
case '6':
englishOperand= "six";
break;
case '7':
englishOperand= "seven";
break;
case '8':
englishOperand= "eight";
break;
case '9':
englishOperand= "nine";
break;
}
return englishOperand;
}
//this method prints the decription of this program.
public static void printIntro()
{
System.out.println("This program is a calculator, you need to enter two");
System.out.println("single digit numbers and an operation(plus, minus,");
System.out.println("divide, multiply, power) and it outputs its numeric");
System.out.println("and English version. Your operand and operation can be");
System.out.println("separated by space(s) or there could be no spaces");
System.out.println("between them. For example you can enter “23plus” or “2 3 plus”");
}
}
2 3 プラスを入力し、5 を期待していましたが、それを受け取りませんでしたが、2 3 マイナスを入力すると、-1 を受け取りました。