gpt4 book ai didi

java - 使用 Scanner 的简单 Java 程序中的编译错误

转载 作者:行者123 更新时间:2023-12-02 09:39:12 26 4
gpt4 key购买 nike

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:");
}
}

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

最佳答案

keyboard.nextInt(); 

而不是

keyboard.next();

如果您想使用keyboard.next();,请将intfirst更改为Stringfirst

将您的代码修改为:

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));
}
}

将类名更改为另一个而不是字符串

关于java - 使用 Scanner 的简单 Java 程序中的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43013784/

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