Java程序从用户输入中查找字符串长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18584228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Program find string length from user input
提问by Eric Grube
public class Strings {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String word1 = sc.next();
System.out.println(word1.length());
sc.close();
}
}
So i want to make a very simple program that takes in a user string and returns the length, but the program does not even run. I am using eclipse. Any tips?
所以我想制作一个非常简单的程序,它接受一个用户字符串并返回长度,但该程序甚至没有运行。我正在使用日食。有小费吗?
回答by Rhompus
Your program works fine.
你的程序运行良好。
Are you forgetting to type something into the console? It might appear to not be running but it actually is.
您是否忘记在控制台中输入内容?它可能看起来没有运行,但实际上是。
ALSO: If you are new to java/programming I would suggest using Netbeans over Eclipse. Netbeans offers a bit more support, although it is less flexible (something you shouldn't need to worry about right now).
另外:如果您不熟悉 Java/编程,我建议您在 Eclipse 上使用 Netbeans。Netbeans 提供了更多的支持,尽管它不太灵活(您现在不需要担心)。
回答by Ruchira Gayan Ranaweera
Make sure that your are providing an input to calculate the length of word.
确保您提供了一个输入来计算单词的长度。
This is working fine if you providing an input, it is better to change your code as follows:
如果您提供输入,这工作正常,最好按如下方式更改代码:
public class Strings {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your word: \n");
String word1 = sc.next();
System.out.println(word1.length());
sc.close();
}
}