为什么java赢了';不打印最后一行吗?

为什么java赢了';不打印最后一行吗?,java,twitter,drjava,Java,Twitter,Drjava,我是java新手,我有一个任务要计算推特中的s(必须在单词的开头)。代码如下: public static void main (String str[]) throws IOException { Scanner scan = new Scanner(System.in); System.out.println("Please enter a tweet."); String tweet=scan.nextLine();

我是java新手,我有一个任务要计算推特中的s(必须在单词的开头)。代码如下:

     public static void main (String str[]) throws IOException {
          Scanner scan = new Scanner(System.in);

          System.out.println("Please enter a tweet.");
          String tweet=scan.nextLine();
          int quantity = tweet.length();
          System.out.println(tweet);
          if (quantity > 140)
          {
            System.out.println("Excess Characters: " + (quantity - 140));
          }
          else{
            System.out.println("Length Correct");
            int hashtags=0;
            int v=0;
            String teet=tweet;
              while ((teet.indexOf('#')!=-1) || v==0){
              v++; 
              int hashnum= teet.indexOf('#');
              if ((teet.charAt(hashnum + 1)!=(' ')) && (teet.indexOf('#')!=-1)) {
                 hashtags++;}
              teet=teet.substring(hashnum,(quantity-1));
                   }
            System.out.println("Number of Hashtags: " + hashtags);
            }
     }
}

编译器没有检测到任何错误,但当我运行它时,它除了打印
(“Hashtags的数量:“+Hashtags”)
之外,什么都做。有人能帮忙吗?谢谢。

您的while循环永远不会退出

而不是

teet=teet.substring(hashnum,(quantity-1));
使用

请允许我谦虚地提出各种改进建议

    public static void main (String args[]) {
    Scanner scan = new Scanner(System.in);

    System.out.println("Please enter a tweet.");
    String tweet = scan.nextLine();
    System.out.println(tweet);

    if (tweet.length() > 140) {
        System.out.printf("Excess Characters: %d%n", tweet.length() - 140);
    } else {
        System.out.println("Length Correct");

        int hashtags = tweet.length() - tweet.replaceAll("#(?=[^#\\s])", "").length();
        System.out.printf("Number of Hashtags: %d%n", hashtags);
    }
}
    public static void main (String args[]) {
    Scanner scan = new Scanner(System.in);

    System.out.println("Please enter a tweet.");
    String tweet = scan.nextLine();
    System.out.println(tweet);

    if (tweet.length() > 140) {
        System.out.printf("Excess Characters: %d%n", tweet.length() - 140);
    } else {
        System.out.println("Length Correct");

        int hashtags = tweet.length() - tweet.replaceAll("#(?=[^#\\s])", "").length();
        System.out.printf("Number of Hashtags: %d%n", hashtags);
    }
}