有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVAutil。扫描器如何从JAVA键盘扫描中的值

我试图编写一个代码,用户在其中输入数字,if语句比较这些值以给出输出。我觉得错误在于我扫描数字的方式。有什么建议吗?(对于noob的问题,也很抱歉,刚刚开始学习java)

public class MarvinsRoom {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter Integer Value");
        firstNumber = scan.nextInt();

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter Integer Value");
        secondNumber = scan.nextInt();

        if (firstNumber > secondNumber) {
            System.out.println("Emotions");
        } else {
            System.out.println("no feelings");
        }
    }
}

共 (3) 个答案

  1. # 1 楼答案

    我相信你的错误是因为你的代码中有两个扫描器对象,你只需要一个,因为它在相同的方法中。据我所知,这应该是你们唯一的问题

  2. # 2 楼答案

    错误:两个扫描变量和变量firstNumber和secondNumber没有类型

    public class MarvinsRoom {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter Integer Value");
            int firstNumber = scan.nextInt();
    
            System.out.println("Enter Integer Value");
            int secondNumber = scan.nextInt();
    
            if (firstNumber > secondNumber) {
                System.out.println("Emotions");
            } else {
                System.out.println("no feelings");
            }
        }
    }
    
  3. # 3 楼答案

    忘了 导入java。util。扫描仪

    最后呢

    扫描。close()