如何在此程序中添加循環以便程序保持執行,然後在用戶輸入介於100和999之間的有效數字時結束?如何添加一個循環到這個程序並讓它保持運行直到程序爲真?
import java.util.*;
public class numLoop{
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int number = 0;
try{
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number between 100 and 999: ");
number = reader.nextInt();
if(number > 999 || number < 100)
System.out.println("\nYour number was invalid.");
else
System.out.println("\nThe number " + number + " was vaild.");
}
catch(InputMismatchException ime){
System.out.println("You didn't enter a number.");
}
}
}
爲做好搜索/而結構。 –
您正在尋找[while循環](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html)。 – thegrinner
實現'while'循環 –