我試圖讓它到的地方,如果數字是負數,它有你重新開始,並輸入另一個數字。我試着做了一段時間的循環,但它只是把System.out.println
,我進入了一個無限循環。我將如何去循環回到開始?
System.out.println("Please input a positive number: ");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
if (num < 0) {
System.out.println("That number is negative.");
}
if (num >= 90 && num <= 110) {
boolean value = true;
} else if (num >= 190 && num <= 210) {
boolean value = true;
}
我之前做過這樣的事情,我記得它很簡單,一旦我明白了哈哈。不幸的是我沒有那個文件。任何幫助?
*編輯begining
int num = scan.nextInt();
while (num < 0)
{
System.out.println("Please input a positive number: ");
Scanner scan = new Scanner(System.in);
}
*編輯#2
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
while (num < 0)
{
System.out.println("Please input a positive number: ");
}
*最終工作代碼
int num;
Scanner scan = new Scanner(System.in);
while (true)
{
System.out.println("Please input a positive number: ");
num = scan.nextInt();
if (num < 0)
{
System.out.println("That number is negative.");
}
else if ((num >= 90 && num <= 110) || (num >= 190 && num <= 210))
{
System.out.println("true");
break;
}
else
{
System.out.println("false");
break;
}
}
向我們展示導致無限循環的代碼。很可能你搞砸了你的'掃描儀' - 例如通過在循環內多次實例化它。 –