Java BlueJ - 我的程序编译没有错误但没有运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23433010/
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
BlueJ - My program compiles with no errors but doesn't run
提问by user3385542
Hello I'm having problems with a program that's supposed to take in a string and then capitalize the first letters of each word using the Character Wrapper class.
您好,我遇到了一个程序问题,该程序应该接收一个字符串,然后使用 Character Wrapper 类将每个单词的第一个字母大写。
import java.util.*;
public class wrapper
{
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
String s1;
s1=input.nextLine();
s1= s1.trim();
int howLong= s1.length();
int i;
int counter=0;
char cho;
for(counter=1; counter<= howLong+1; counter++)
{
cho=s1.charAt(counter);
if(Character.isLetter (cho) && ! Character.isLetter(s1.charAt(counter-1)))
{
System.out.print( Character.toUpperCase(cho) );
}
else
{
System.out.print(cho);
}
System.out.println();
}
}
}
That's the program so far, but while it compiles with no errors according to BlueJ, it doesn't run. Any help as to why this is happening would be great.
到目前为止的程序就是这样,但是虽然根据 BlueJ 编译没有错误,但它并没有运行。关于为什么会发生这种情况的任何帮助都会很棒。
Edit: Changed the program to what I believe would make it not just print out the spaces that the char variable was initialized to, but it still does not run. Maybe there's something wrong with the loop?
编辑:将程序更改为我认为不仅可以打印出 char 变量初始化为的空格,而且仍然无法运行的程序。也许循环有问题?
回答by Scott Hetrick
The reason your program compiles but doesn't run is because of the line s1=input.nextLine();
. At this line, the program is waiting for input from the user to use as the string s1
, but does not show the terminal in order for the user to give such input. A way you can get around this is to force the terminal to show itself before that line. I would recommend putting something like
您的程序编译但不运行的原因是因为s1=input.nextLine();
. 在这一行,程序正在等待用户的输入以用作 string s1
,但不显示终端以便用户提供此类输入。您可以解决此问题的一种方法是强制终端在该行之前显示自己。我建议放一些类似的东西
System.out.println("Enter input:");
System.out.println("Enter input:");
before the line, so that the terminal will show itself & the user can enter input into it. From there, you can work on the program like you would normally.
在该行之前,以便终端显示自己并且用户可以在其中输入输入。从那里,您可以像往常一样处理程序。