简体   繁体   English

BlueJ-我的程序编译没有错误,但是没有运行

[英]BlueJ - My program compiles with no errors but doesn't run

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. 到目前为止,这是程序,但是根据BlueJ编译时没有错误,但它没有运行。 Any help as to why this is happening would be great. 关于为什么会发生这种情况的任何帮助都将是巨大的。

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. 编辑:将该程序更改为我认为将使它不仅打印出char变量已初始化为的空格,而且仍然无法运行。 Maybe there's something wrong with the loop? 循环可能有问题吗?

The reason your program compiles but doesn't run is because of the line s1=input.nextLine(); 您的程序编译但无法运行的原因是因为行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. 在这一行,程序正在等待来自用户的输入以用作字符串s1 ,但并未显示终端,以便用户提供此类输入。 A way you can get around this is to force the terminal to show itself before that line. 解决此问题的一种方法是强制终端在该行之前显示自己。 I would recommend putting something like 我建议您将类似

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. 从那里,您可以像往常一样处理该程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM