询问整数作为输入并退出如果q但如果任何其他字母再试

我正在研究一个程序,它会询问用户一个整数,然后另一个,并把它们作为图点。它会告诉他们之间的距离。用户必须输入一个整数或按“Q”退出。如果是其他内容(不是整数或字母“Q”),它会告诉他们这是不正确的,请再试一次。我认为这是我可以做到的,但它会返回错误cannot find symbol。帮助非常感谢!询问整数作为输入并退出如果q但如果任何其他字母再试

import java.util.Scanner; 

public class PointCheck{

public static void main(String[] args){

try {

System.out.println("Welcome");

System.out.println("To quit at anytime please type \"Q\". Enter point:");

char Q = 'Q';

char q = 'q';

Scanner scan = new Scanner (System.in);

input = scan.next();

while (input.hasNext()) {

if (input.hasNextInt()) {

System.out.println("Int input");

}

else if (input.equals("Q")) {

System.out.println("Exiting");

}

/*else {

System.out.println("You did not enter a valid value. Please enter a number or \"Q\" to quit.");

}*/

}

}

catch(Exception e){

System.out.println("Exiting Program.");

}

}

}

如果我不注释掉最后一个else语句,它只是默认为那个,并且永远循环我的错误信息。

回答:

我使你的代码进行一些修改,希望它能帮助:)

public class PointCheck { 

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

try {

System.out.println("Welcome");

System.out.println("To quit at anytime please type \"Q\". Enter point:");

Scanner scan = new Scanner(System.in);

while (scan.hasNext()) {

if (scan.hasNextInt()) {

System.out.println("Int input" + scan.nextInt());

} else {

String input = scan.next();

if (input.equalsIgnoreCase("Q")){

System.out.println("Exiting");

break;

}else {

System.out.println("You did not enter a valid value. Please enter a number or \"Q\" to quit.");

}

}

}

} catch (Exception e) {

System.out.println("Exiting Program.");

}

}

}

回答:

你可以试试这个... 的投入将只需要整数,如果输入以外的任何其他一个Integer,它将抛出一个异常并移动到程序将终止的catch块。

import java.util.Scanner; 

public class demo {

public static void main(String[] args) {

System.out.println("Welcome");

System.out.println("Press any key to exit.. Enter point:");

Scanner scanner= new Scanner(System.in);

while (true){

try {

int number = scanner.nextInt();

System.out.println(number);

}

catch (Exception e){

System.exit(0);

}

}

}

}

回答:

像这样的东西可能是你喜欢的。阅读代码中的注释:

System.out.println("Welcome"); 

Scanner scanner= new Scanner(System.in);

int number = 0;

while (true){

System.out.println("Enter point (q to quit): ");

String strg = scanner.nextLine();

// Get out of loop if q or Q or quit,

// or QUIT, or Quit, etc is entered.

// providing the input actually contains

// something.

if (!strg.equals("") && strg.toLowerCase().charAt(0) == 'q') {

break;

}

// Use the String.matches() method with regex to

// determine if an integer number was supplied.

if (!strg.matches("\\d+")) {

System.err.println("Invalid Entry - Integer values only! Try again.\n");

continue; // Start loop from beginning.

}

// Convert string number to Integer.

number = Integer.parseInt(strg);

break; // Get outta loop

}

String msg = "The number entered is: " + number;

if (number == 0) { msg = "Nothing Entered!"; }

System.out.println(msg);

以上是 询问整数作为输入并退出如果q但如果任何其他字母再试 的全部内容, 来源链接: utcz.com/qa/259555.html

回到顶部