Java 扫描仪需要/正在请求输入两次
我只是在写一个小程序,从用户那里接收输入,然后打印回用户。但是,当我运行程序时,它会要求输入两次(它会打印初始语句,然后键入并按enter键后,什么也不会发生,但如果再次执行,它会工作并打印。)顶部值显然没有权重或意义,因为它没有显示,但第二个值打印得很好Java 扫描仪需要/正在请求输入两次,java,string,input,Java,String,Input,我只是在写一个小程序,从用户那里接收输入,然后打印回用户。但是,当我运行程序时,它会要求输入两次(它会打印初始语句,然后键入并按enter键后,什么也不会发生,但如果再次执行,它会工作并打印。)顶部值显然没有权重或意义,因为它没有显示,但第二个值打印得很好 package kek; import java.util.Scanner; public class Kek { public static void main (String[] args){ Scanner
package kek;
import java.util.Scanner;
public class Kek {
public static void main (String[] args){
Scanner input = new Scanner(System.in);
System.out.println("What is kek? (Top, bottom, etc.)");
String s1 = input.next();
if (input.hasNext("kek")) {
System.out.println("No.");
System.exit(0);
} else {
System.out.println(s1 + "kek");
input.close();
}
}
}
这是控制台:
What is kek? (Top, bottom, etc.)
top
top
topkek
及
我正在使用eclipse开普勒。删除
input.next()代码>在代码和使用中。input.next()
获取一个输入,并且具有.next()
获取其他输入首先将一个值读入s1
,然后告诉扫描仪
在其中某处查找另一行值为“kek”
。也许你的意思是说if(input.equals(“kek”)
?不确定,但这就是你的意思吗
public static void main(String[] args) {
System.out.println("What is kek? (Top, bottom, etc.)");
Scanner input = new Scanner(System.in);
String string = input.nextLine();
if(string.equals("kek")){
System.out.println("No.");
System.exit(0);
}else{
System.out.println(string + "kek");
input.close();
}
}
如果没有,那么你需要更具体地说明你想要实现什么。从您现在所写的内容来看,有点不清楚您真正想要什么您使用next()
和hasNext()
两次请求输入。只需执行以下操作即可:
package kek;
import java.util.Scanner;
public class Kek {
public static void main (String[] args){
Scanner input = new Scanner(System.in);
System.out.println("What is kek? (Top, bottom, etc.)");
String s1 = input.next();
//just compare strings using the .equals() method instead
//of using the hasNext() which will ask for another input
if ("kek".equals(s1)) {
System.out.println("No.");
System.exit(0);
}
else{
System.out.println(s1 + "kek");
input.close();
}
}
}
我遇到了完全相同的问题,尽管当我键入“否”或“否”时只需要输入两次。。。我不知道为什么只有“否”区域受到影响
import java.util.Scanner;
public class Methods2 {
static Scanner userInput = new Scanner(System.in);
static String answer;
static int yes1no2;
static int loopStage = 1;
public static void main(String[] args)
{
System.out.println("Welcome to the bank.\nWould you like to open an account?");
while (loopStage == 1)
{
if (yesOrNo() == 1)
{
loopStage = 2;
System.out.println("Great.");
}
else if (yesOrNo() == 2)
{
System.out.println("Ok, please come again.");
}
}
}
public static int yesOrNo()
{
answer = userInput.nextLine();
if (answer.equalsIgnoreCase("yes"))
{
yes1no2 = 1;
}
else if (answer.equalsIgnoreCase("no"))
{
yes1no2 = 2;
}
else
{
yes1no2 = 3;
System.out.println("Please say 'Yes' or 'No'.");
}
return yes1no2;
}
}实际上我最初使用的是input.equals,但是如果我输入“kek”,它不会返回“no”,只返回最后一个字符串。(kekkek)@user2795691看起来你没有使用调试器,所以你当时可能没有检查变量的值,但我怀疑你最初没有修剪换行符。这就成功了,谢谢!不知何故,我忽略了我在使用input.Next(.)input.hasNext.@user2795691 cool:)如果你觉得这个答案有帮助,请接受它:)我正在冷却,你们帮了我很大的忙,太快了,以至于我不得不等我才能接受答案!:)我很抱歉,如果这不是正确的地方张贴这个。这是我第一次使用这个网站。这是银行的控制台。您想开个账户吗?不,不,好的,请再来。
import java.util.Scanner;
public class Methods2 {
static Scanner userInput = new Scanner(System.in);
static String answer;
static int yes1no2;
static int loopStage = 1;
public static void main(String[] args)
{
System.out.println("Welcome to the bank.\nWould you like to open an account?");
while (loopStage == 1)
{
if (yesOrNo() == 1)
{
loopStage = 2;
System.out.println("Great.");
}
else if (yesOrNo() == 2)
{
System.out.println("Ok, please come again.");
}
}
}
public static int yesOrNo()
{
answer = userInput.nextLine();
if (answer.equalsIgnoreCase("yes"))
{
yes1no2 = 1;
}
else if (answer.equalsIgnoreCase("no"))
{
yes1no2 = 2;
}
else
{
yes1no2 = 3;
System.out.println("Please say 'Yes' or 'No'.");
}
return yes1no2;
}