gpt4 book ai didi

java - 为什么它不要求我输入?

转载 作者:行者123 更新时间:2023-12-01 07:27:17 26 4
gpt4 key购买 nike

import java.util.Scanner;

class Tutorial {
public static void main (String args[]){
System.out.println("Who goes there?");
Scanner name = new Scanner(System.in); ## I am asking for input form user but it does not take imput
if (name.equals("me") || name.equals("Me") ){
System.out.println("Well, good for you smartass.");
}else System.out.println("Well good meet");
}
}

为什么程序运行 else 而不要求我输入?

最佳答案

您应该使用 scanner.nextLine() 读取您的输入:

Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
if (name.equals("me") || name.equals("Me"))
{
System.out.println("Well, good for you smartass.");
} else {
System.out.println("Well good meet");
}
scanner.close();

关于java - 为什么它不要求我输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22280419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com