【问题标题】:For loop is printing out multiple print statementsFor循环正在打印出多个打印语句
【发布时间】:2020-06-13 20:14:47
【问题描述】:

我正在为课程制作一个程序,它可以打印出单词中元音的数量,如果有任何帮助,我们将不胜感激。目前,程序打印出正确数量的元音,但之前多次打印出打印语句“vowels:”。我试过移动打印语句和大括号,但它说“错误:'else if'没有'if'”。如果解决方案显而易见,我对 Java 完全陌生,很抱歉。提前谢谢你:)

      import java.util.Scanner;
         public class Main
        {
             public static void main(String[] args) {
                Scanner input = new Scanner(System.in);
                System.out.print("Enter text: ");
                String text = input.nextLine();
                text = text.toLowerCase();
                int vowels= 0;
                int l;
                l= text.length();

               for (int i = 1; i < text.length(); i++) { 
                 String wordPRT = text.substring(i,i+1);
                  if (wordPRT.compareToIgnoreCase("a")==0 || wordPRT.compareToIgnoreCase("e")==0|| 
                     wordPRT.compareToIgnoreCase("i")==0
                      || wordPRT.compareToIgnoreCase("o")==0
                      || wordPRT.compareToIgnoreCase("u")==0){
                         vowels++;

                    System.out.println("vowels: " + vowels);
                 }
                 else if(vowels<1){

               System.out.print("no vowels");

                }
              }
             }
            }









【问题讨论】:

    标签: java loops printing


    【解决方案1】:

    您在 for 循环中打印所有内容,而不是计算元音并在最后打印。

    尝试类似:

    int vowelsCounter = 0;
    for(...) {
      ... logic to count the vowels
      if(isvowel(string.charAt(i)){
         vowelsCountr++;
      }
    }
    
    if(vowelsCounter > 0 ) {
       printSomething
    }
    else {
      print something else
    }
    

    此外,对于这种循环,您不应使用 subString,而应使用 string.charAt(i)

    【讨论】:

      【解决方案2】:

      将打印语句移出for 循环。

      import java.util.Scanner;
      
      public class Main {
          public static void main(String[] args) {
              Scanner input = new Scanner(System.in);
              System.out.print("Enter text: ");
              String text = input.nextLine();
              text = text.toLowerCase();
              int vowels = 0;
              int l;
              l = text.length();
      
              for (int i = 1; i < text.length(); i++) {
                  String wordPRT = text.substring(i, i + 1);
                  if (wordPRT.compareToIgnoreCase("a") == 0 || wordPRT.compareToIgnoreCase("e") == 0
                          || wordPRT.compareToIgnoreCase("i") == 0 || wordPRT.compareToIgnoreCase("o") == 0
                          || wordPRT.compareToIgnoreCase("u") == 0) {
                      vowels++;    
                  }
              }
              if (vowels >= 1) {
                  System.out.println("vowels: " + vowels);
              } else {
                  System.out.print("no vowels");
              }
          }
      }
      

      示例运行:

      Enter text: Hello
      vowels: 2
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-18
        • 2017-07-31
        • 2021-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多