我正在嘗試為學校功能構建一些培訓應用程式。問題是:每次隨機選擇的數字小于0時,我的函式都會顯示 -,因為我的函式有固定的格式。 例子
我嘗試使用 Intl-Package 的 NumberFormat,但是我無法正確使用 int 值。有沒有辦法為正數顯示一個加號,同時它們仍然可以使用它們?
到目前為止的代碼:
int randomNumberMinMax(int min, int max){
int randomminmax = min Random().nextInt(max - min);
if(randomminmax==0){
randomminmax = min Random().nextInt(max - min);
}
//generate random number within minimum and maximum value
return randomminmax;
}
int a = randomNumberMinMax(-5, 5);
int b = randomNumberMinMax(-10, 10);
int c = randomNumberMinMax(-10, 10);
String task = "f(x) = $a(x $b)2 $c";
uj5u.com熱心網友回復:
例如,當數字為正時,您只能顯示加號
String task = "f(x) = $a(x${b >= 0 ? " " : ""}$b)2${c >= 0 ? " " : ""} $c";
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/513076.html
標籤:扑数字格式
