사용자가 1과 10 사이의 숫자를 입력 한 다음 숫자를 표시하도록 요청하는 간단한 프로그램을 작성하려고합니다. 이제 숫자가 범위를 벗어나면 다시 질문 할 수 있지만 숫자가 아닌 다른 것이 입력되면 다시 묻을 수는 없습니다 (예 : %
또는 hello
).JOptionPane 입력을 정수로 변환
소스 코드 : (상단을 잘라)
public static void main(String[] args){
int number; //For holding the number
String stringInput; //For holding the string values until converted
//------------------//
//Introducing the user
JOptionPane.showMessageDialog(null, "This is a program that will ask \n"
+ "you to enter a number in-between \n"
+ "1-10, if the number is not within \n"
+ "the parameters, the program will repeat.");
//---------------------//
//Get input from the user
stringInput = JOptionPane.showInputDialog("Enter number.");
number = Integer.parseInt(stringInput);
//-----------------//
//Checking the number
while (number > 10 || number < 0){
stringInput = JOptionPane.showInputDialog("That number is not within the \n"
+ "allowed range! Enter another number.");
number = Integer.parseInt(stringInput);
}
//-------------------//
//Displaying the number
JOptionPane.showMessageDialog(null, "The number you chose is "
+ number
+ ".");
//-------------//
//Closing it down
System.exit(0);
}
가장 큰 문제가 있습니다 : 내가 제대로 데이터 값을 변환 할 수없는 것
number = Integer.parseInt(stringInput);
. 이미 if 문을 사용하여 숫자가 정수인지 확인하는 것과 같은 것을 생각했지만 확인 방법을 알 수 없었습니다. 내가 할 수 있으면 좋겠다 :
if (number == Integer)
나는 아직도 자바에 매우 익숙하지 않다. 어떤 도움을 주셔서 감사합니다. 시간을내어 읽어 주셔서 감사합니다.