我的问题是我要用户输入一个正整数的值。我确保他们不能输入负整数或零,但我不确定如何确保他们不能输入小数。
package java1;
public class Problem3NPlus1 {
public static void main(String[] args) {
int givenInt;
int givenIntMod;
System.out.println("Provide a positive integer: ");
givenInt = TextIO.getlnInt();
while (givenInt <= 0 || *IF GIVENINT IS A DECIMAL? ) {
System.out.println("That is not a positive integer, try again: ");
givenInt = TextIO.getlnInt();
}
if (givenInt > 0) {
while (givenInt != 1) {
if (givenInt % 2 == 0) {
givenIntMod = (givenInt / 2);
}
else {
givenIntMod = ((givenInt * 3) + 1);
}
System.out.println(givenIntMod);
givenInt = givenIntMod;
}
}
}
}
最佳答案
当TextIO.getlnInt()
返回一个int值时,返回的内容将是一个int,并且int不能有小数。您要查看TextIO.getlnInt()
方法。您能告诉我们这是如何工作的吗?