需要帮助跳转到 Java 以及从 C++ 进行 Java 输入
这是我用 java 编写的第一个程序,我还没有找到任何好的网站,例如 这个 C++ 网站,这很令人困惑对我来说,因为我刚刚开始编写 java,而且我刚刚从 C++ 转过来。无论如何,关于这段代码,有人可以解释如何修复此代码,因为包含 Scanner
的行和/或如何简单地接收输入,因为我还没有找到任何简单的方法来翻译 来自 C++ 的 cin >>
public class input {
public static void main(String[] args) {
double total = 0;
Scanner in = new Scanner(System.in);
System.out.println("As you enter numbers, they will be added.");
System.out.println("Entering a non-number will stop the program.");
while (in.hasNextDouble()) {
double n = in.nextDouble();
total = total + n;
System.out.println("The total is " + total);
}
}
}
This is my first program in java and I haven't found any good websites like this one for C++ and it's confusing for me because I just started writing java and I just came from C++. Anyways, concerning this code, could someone explain how to fix this code because of the line containing Scanner
and/or how to simply receive inputs, because I haven't found any simple way to translate cin >>
from C++
public class input {
public static void main(String[] args) {
double total = 0;
Scanner in = new Scanner(System.in);
System.out.println("As you enter numbers, they will be added.");
System.out.println("Entering a non-number will stop the program.");
while (in.hasNextDouble()) {
double n = in.nextDouble();
total = total + n;
System.out.println("The total is " + total);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的代码有效。只需确保您有
import java.util.Scanner
。与此相关的是,请使用 Eclipse 或 Netbeans,因为它们会告诉您这一点。另外,您应该将类名大写并将类放入包中而不是“默认包”中。我推荐“Head First Java”。这是我运行时的输出。我想我可能会认为这是一个错误,我能够用空行按回车键而不会结束。
Your code works. Just make sure you have
import java.util.Scanner
. On a related note, use Eclipse or Netbeans as they would have told you this. Also, you should capitalize class names and put your class in a package instead of in the "default package". I recommend "Head First Java".Here is output when I ran it. I think I might consider it a bug that I was able to hit enter with a blank line without it ending.
我在工作,没有安装jdk,所以无法编译运行。不过,快速浏览一下,这似乎是您在摆脱扫描仪时可能遇到的唯一问题。输入几个数字后,尝试按 ctrl-d - 这应该表示输入结束。
I'm at work and don't have the jdk installed, so I can't compile and run this. Giving a quick look though, it seems like the only thing you might have problem breaking out of the scanner, though. After entering a few numbers, try pressing ctrl-d - this should signal end of input.
正如 Borealid 所说,您需要在类的顶部添加以下行才能编译它:
另请注意,按照 java 类的约定,类以大写字符“Input”命名,而不是“input”。
最后,您可以通过 System.in.read() 和 read() 方法的其他重载排列直接获取输入,而不是 System.in
As Borealid said you need to add the following line at the top of the class to get it to compile:
Also note that by convention in java classes are named with an uppercase character Input, not input.
Finally, you can obtain input directly through System.in.read() and the other overloaded permutations of the read() method, against System.in
查看 Java 教程,它们非常适合初学者
Check out the Java Tutorials,they're quite good for a beginner