在Java中,你可以使用條件語句(if-else語句)來設定數值的范圍。下面是一個示例:
int number = 10;
if (number >= 0 && number <= 10) {
System.out.println("Number is between 0 and 10.");
} else if (number > 10 && number <= 20) {
System.out.println("Number is between 11 and 20.");
} else {
System.out.println("Number is outside the specified range.");
}
上述代碼檢查變量number
是否在0到10之間,如果是,則打印"Number is between 0 and 10.“。如果不在該范圍內,程序會繼續檢查是否在11到20之間,如果是,則打印"Number is between 11 and 20.”。如果不在任何范圍內,則打印"Number is outside the specified range."。
你可以根據自己的需求修改范圍條件。