}
}
});
}
private String evaluateExpression() {
String expression = inputStringBuilder.toString();
String[] tokens = expression.split(" ");
double operand1 = Double.parseDouble(tokens[0]);
String operator = tokens[1];
double operand2 = Double.parseDouble(tokens[2]);
double result = 0;
switch (operator) {
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "*":
result = operand1 * operand2;
break;
case "/":
if (operand2 != 0) {
result = operand1 / operand2;
} else {
throw new ArithmeticException("Division by zero");
}
break;
}
return String.valueOf(result);
}
private void updateResultText() {
resultTextView.setText(inputStringBuilder.toString());
}
}
activity_main.xml:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
android:id="@+id/resultTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Do'stlaringiz bilan baham: |