我需要制作一个程序,允许我向用户询问一些数字并继续这样做,直到用户输入'q',但我似乎无法让hasNextInt像我认为的那样工作。我对Java很陌生,并且在做地面课程。如何将一个扫描仪用于整数和文本?
这是我迄今所做的
public class Matte
{
public static void main (String[] args)
{
int r1 = 0;
int h1 = 0;
int r2 = 0;
int h2 = 0;
int level = 1;
String choice = new String();
Scanner values = new Scanner(System.in);
values.useDelimiter("\\s");
if (level == 1)
{
if(!values.hasNextInt())
r1 = values.nextInt();
else choice = values.nextLine();
if(!values.hasNextInt())
h1 = values.nextInt();
else choice = values.nextLine();
if(!values.hasNextInt())
r2 = values.nextInt();
else choice = values.nextLine();
if(!values.hasNextInt())
h2 = values.nextInt();
else choice = values.nextLine();
}
}
}
我要救什么都文本扫描仪输入在“选择”可变
如何做呢?
您能否提供样品输入和样品输出? –
看起来很奇怪,你检查下一个是否是int,如果不保存为整数? 'String.valueOf(values.nextLine())' – cYrixmorten
如何在'hasNextInt()'为** false **时调用'nextInt()'? – shmosel