Java:输入比较
我想知道为什么当我在被问到这个方法中是否还有其他数字后键入“y”时,代码会正常工作并离开循环,但在键入“n”并点击返回时,我需要再次键入“n”并点击返回,否则进程就会挂起 为什么? 字符串“input”是从用户在main方法中的输入传递过来的,在本例中是“addition” 我通过使用Java:输入比较,java,string,compare,Java,String,Compare,我想知道为什么当我在被问到这个方法中是否还有其他数字后键入“y”时,代码会正常工作并离开循环,但在键入“n”并点击返回时,我需要再次键入“n”并点击返回,否则进程就会挂起 为什么? 字符串“input”是从用户在main方法中的输入传递过来的,在本例中是“addition” 我通过使用 System.out.println("Any more digits? Type y/n"); String yayOrNay = scan.next(); if (yayOrNay.length()==1
System.out.println("Any more digits? Type y/n");
String yayOrNay = scan.next();
if (yayOrNay.length()==1 && yayOrNay.charAt(0)=='y')
{
keepGoing = true;
}
但这对我来说似乎有点太复杂了。scan.next()
从输入流中提取一个新字符
因此,当您检查“n”和scan.next().matches(“Y | Y”)
执行时,它实际上跳过了“n”进行下一次比较
解决方案是将scan.next()
分配到一个可以使用的变量中:
while (keepGoing == true)
{
System.out.println("Next digit = (Type the digit)");
additionValue = additionValue + scan.nextInt();
System.out.println("Any more digits? Type y/n");
String next = scan.next();
if (next.matches("Y|y"))
{
keepGoing = true;
}
else if (next.matches("N|n"))
{
keepGoing = false;
}
else
{
System.out.println("Great, you broke it.");
System.exit(1);
}
}
scan.next()
从输入流中提取一个新字符
因此,当您检查“n”和scan.next().matches(“Y | Y”)
执行时,它实际上跳过了“n”进行下一次比较
解决方案是将scan.next()
分配到一个可以使用的变量中:
while (keepGoing == true)
{
System.out.println("Next digit = (Type the digit)");
additionValue = additionValue + scan.nextInt();
System.out.println("Any more digits? Type y/n");
String next = scan.next();
if (next.matches("Y|y"))
{
keepGoing = true;
}
else if (next.matches("N|n"))
{
keepGoing = false;
}
else
{
System.out.println("Great, you broke it.");
System.exit(1);
}
}
你再打两次扫描
第一次检查是否为Y,如果不是,请再次读取输入
所以
你再打两次扫描
第一次检查是否为Y,如果不是,请再次读取输入
所以
您的错误是调用scan.next()两次,它请求两次输入
private int add(String input)
{
int additionValue = 0;
boolean keepGoing = true;
if (input.matches("addition"))
{
while (keepGoing == true)
{
System.out.println("Next digit = (Type the digit)");
additionValue = additionValue + scan.nextInt();
System.out.println("Any more digits? Type y/n");
String s = scan.next();
if (s.matches("Y|y"))
{
keepGoing = true;
}
else if (s.matches("N|n"))
{
keepGoing = false;
}
else
{
System.out.println("Great, you broke it.");
System.exit(1);
}
}
}
}
您的错误是调用scan.next()两次,它请求两次输入
private int add(String input)
{
int additionValue = 0;
boolean keepGoing = true;
if (input.matches("addition"))
{
while (keepGoing == true)
{
System.out.println("Next digit = (Type the digit)");
additionValue = additionValue + scan.nextInt();
System.out.println("Any more digits? Type y/n");
String s = scan.next();
if (s.matches("Y|y"))
{
keepGoing = true;
}
else if (s.matches("N|n"))
{
keepGoing = false;
}
else
{
System.out.println("Great, you broke it.");
System.exit(1);
}
}
}
}
这是因为您调用了scan.next()两次。 您必须调用它一次,将结果字符串放入变量中
String input2 = scan.next();
if (input2.matches("Y|y"))
{
keepGoing = true;
}
else if (input2.matches("N|n"))
{
keepGoing = false;
}
这是因为您调用了scan.next()两次。 您必须调用它一次,将结果字符串放入变量中
String input2 = scan.next();
if (input2.matches("Y|y"))
{
keepGoing = true;
}
else if (input2.matches("N|n"))
{
keepGoing = false;
}
您需要输入'n'两次,因为您在每个if条件下扫描输入。因此,如果字母不是“y”,程序将在下一个if语句中等待用户输入 您可以简单地执行以下操作:
String yayOrNay = scan.next();
if (yayOrNay.matches("Y|y"))
{
keepGoing = true;
}
else if (yayOrNay.matches("N|n"))
{
keepGoing = false;
}
...
您需要输入'n'两次,因为您在每个if条件下扫描输入。因此,如果字母不是“y”,程序将在下一个if语句中等待用户输入 您可以简单地执行以下操作:
String yayOrNay = scan.next();
if (yayOrNay.matches("Y|y"))
{
keepGoing = true;
}
else if (yayOrNay.matches("N|n"))
{
keepGoing = false;
}
...
请试试这个
Scanner scan = new Scanner(System.in);
int additionValue=0;
while (true)
{
System.out.println("Any more digits? Type y/n");
if (scan.next().matches("Y|y"))
{
System.out.println("Next digit = (Type the digit)");
additionValue = additionValue + scan.nextInt();
//System.out.println("Any more digits? Type y/n");
}
else
{
System.out.println("Thanks");
break;
}
}
我不确定这种逻辑是否适用于你。”如果需要,还可以处理“n”
输出
Any more digits? Type y/n
y
Next digit = (Type the digit)
5
Any more digits? Type y/n
n
Thanks
请试试这个
Scanner scan = new Scanner(System.in);
int additionValue=0;
while (true)
{
System.out.println("Any more digits? Type y/n");
if (scan.next().matches("Y|y"))
{
System.out.println("Next digit = (Type the digit)");
additionValue = additionValue + scan.nextInt();
//System.out.println("Any more digits? Type y/n");
}
else
{
System.out.println("Thanks");
break;
}
}
我不确定这种逻辑是否适用于你。”如果需要,还可以处理“n”
输出
Any more digits? Type y/n
y
Next digit = (Type the digit)
5
Any more digits? Type y/n
n
Thanks
太棒了,谢谢你的回复,我现在明白我的意思了!太棒了,谢谢你的回复,我现在明白我的意思了!