使用Scanner在简单Java程序中编译错误

Rin*_*ino 2 java compilation

import java.util.*;
public class strings {
    public static void main (String [] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Type your first integer: ");
        int first = keyboard.next();
        System.out.print("Type your seconds integer : ");
        int second = keyboard.next();
        System.out.print("The sum of your two integers are:");
    }
}
Run Code Online (Sandbox Code Playgroud)

我不知道为什么我在字符串上得到2个错误无法转换为int.

Joh*_*Joe 12

keyboard.nextInt(); 
Run Code Online (Sandbox Code Playgroud)

代替

keyboard.next();
Run Code Online (Sandbox Code Playgroud)

如果要使用keyboard.next();,请更改 int firstString first.

将代码修改为:

import java.util.*;
public class Demo {
    public static void main (String [] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Type your first integer: ");
        int first = keyboard.nextInt();

        System.out.print("Type your seconds integer : ");
        int second = keyboard.nextInt();

        System.out.print("The sum of your two integers are:" +(first+second));
    }
}
Run Code Online (Sandbox Code Playgroud)

将您的班级名称更改为其他名称而不是string.