1
import java.util.Scanner;
public class Improvedd {
public static void main (String[] args) {
Scanner input = new Scanner (System.in);
int operand1 = Integer.parseInt (input.nextLine());
char expo1 = input.next().charAt (0);
int operand2 = Integer.parseInt (input.nextLine());
String expression = "operand1 + expo1 + operand2";
// how would I make it so the program ask for a math function
// Like just let them enter 1/2 and get the printed result
// I don't understand how I could just get the string to equal
// what I've placed
System.out.println ("Enter expression: ");
String expression = input.nextLine();
System.out.println (operand1 + expo1 + operand2 + "=");
// if (expo1 == '/' && operand2 == '0') {
// System.out.println ("Cannot divide by zero"); }
// how could I fix this expression
if (expo1 == '-') {
System.out.println (operand1-operand2);
} else
if (expo1 == '+') {
System.out.println (operand1+operand2);
} else
if (expo1 == '/') {
System.out.println (operand1/operand2);
} else
if (expo1 == '%') {
System.out.println (operand1%operand2);
}
else{
System.out.println ("Error.Invalid operator.");
}
}
}
我想要做的幾乎是進入數學運算出現,只是讓他們進入操作是它,%等,但我希望他們輸入它像2/2或3%2,打印結果爲2/2 = 1或3%2 = 1。我遇到的真正麻煩是設置字符串,我不知道如何設置它。與字符串丟失
你在出操作數註釋塊是一個int,所以它與0的比較,而不是與「0」這是個字符。 –