사용자가 숫자 대신 x
을 입력 할 때까지 내 프로그램 루프를 만들고 싶습니다. while 문을 사용하여 시도했지만 여러 변수와 함께 사용하는 방법을 모르겠습니다. 여기 내 코드는x를 입력 할 때까지 Java 프로그램 루프를 만드시겠습니까?
public static void main(String[] args)
{
int denominatorOne = 1, numeratorOne = 1;
System.out.println("Welcome, type an \"x\" at any point to exit the program");
while (numeratorOne !=x)
{
Scanner in = new Scanner(System.in);
//Prompt the user for fraction one
System.out.print("Enter the first numerator (top number): ");
numeratorOne = in.nextInt();
System.out.print("Enter the first denominator (bottom number): ");
denominatorOne = in.nextInt();
}
}
내 할당에서 정확한 말씨는) 그냥 (주이 경우 다시 통해 메소드를 호출 할 수 The program should run in loop and allow the user to exit with some special character input (e.g. x or X to exit)
로 변경합니다'x'가 숫자가 아닌 ... – MadProgrammer
은 'X'는 정수는 따라서 아니다 입력을 문자열로 읽지 않으면 여기서 센티널 값이 될 수 없습니다. if (input.equals ('x')) break;를 호출하고 그렇지 않으면 Integer.parseInt (input);을 수행 할 수 있습니다. – SpencerD
WrapperClass에 대해 알고 계십니까? 입력이 정수가 아니면 구문 분석을 시도하여 사용자가 다른 형식을 입력했는지 확인하여 예외가 발생하면 루프를 잡아서 종료하십시오 ... – Abubakkar