【问题标题】:Java code not processing inputJava 代码不处理输入
【发布时间】:2016-11-28 23:50:04
【问题描述】:

对于学校,我必须制作一个扫描仪,它能够接受字符串输入并计算元音的数量,如 1“a”等。我的问题是,当我输入一个字符串时,它不会通过循环和我可以连续输入并输入什么都不做。 (顺便说一下,我正在使用 BlueJ 进行编码)。感谢您的帮助!

package VowelCounter;

/**
 * Description:
 * Author: Jack Cannon
 * Date: 11/15/16
 */

import java.util.Scanner;
public class VowelCounter
{
public static void main(String [] args)
{
     Scanner input = new Scanner(System.in);
     //Prompt the user to enter a phrase 
     System.out.println("Type in a sentence.");
     String phrase = input.nextLine();
     //as long as the string isn't "quit", 
     //count the vowels in the string.
     int length = phrase.length();
     while(phrase != "quit")
    {
         int a = 0;
         int e = 0;
         int i = 0;
         int o = 0;
         int u = 0;  
       for(int c = 0; length < 1; length++)
       {  
           char vowels = phrase.charAt(c);
         if(vowels == 'a')
         {
         }
         else if(vowels == 'e')
         {
         }
         else if(vowels == 'i')
         {
         }
         else if(vowels == 'o')
         {
         }
         else if(vowels == 'u')
         {
         }
       }
   }    
   System.out.println("There are " + 'a' + "a's");
   System.out.println("There are " + 'e' + "e's");
   System.out.println("There are " + 'i' + "i's");
   System.out.println("There are " + 'o' + "o's");
   System.out.println("There are " + 'u' + "u's");
   //print out the count of the vowels for this string.      
   //prompt the user for another phrase.
   }
  }

【问题讨论】:

  • 您只要求输入一次...在比较 java 中的字符串时也使用字符串 equals 方法
  • 你有什么理由在循环中使用 length

标签: java bluej


【解决方案1】:

您在上面分享的代码有一些问题。

首先,您的 while 循环的条件为 phrase != "quit"。这是错误的,原因有两个。首先,这不是您在 java 中比较字符串的方式。为此,请参阅this。第二个原因是您从未在 while 循环中更改变量 phrase,因此这将导致无限循环,因为 phrase 永远不会“退出”,除非这是用户输入的内容。如果您只需要用户输入一次,我认为不需要这个 while 循环。

您的代码错误的第二个原因是您的 for 循环中的 if 语句(查看当前字符是什么字符)没有执行任何操作。在这一点上,它们只是空的代码块。您最有可能需要做的是增加相应的元音计数器。

最后,您在打印行中使用了不正确的字符串连接方法。 Java中正确的连接方式如下:

"There are " + a + "a's" // a = 1
=> "There are 1 a's"

单引号的使用不正确;如果您想打印字母“a”,可以使用它,如下所示:

"There are a a's"

【讨论】:

    【解决方案2】:

    这应该可以解决您的问题,我只在循环中添加了 2 个 if 语句,您可以添加其余的。

    public static void main(String [] args){
        Scanner input = new Scanner(System.in);
        //Prompt the user to enter a phrase 
        System.out.println("Type in a sentence.");
        String phrase = input.nextLine();
        //as long as the string isn't "quit", 
        //count the vowels in the string.
        int length = phrase.length();
        int a = 0;
        int e = 0;
        int i = 0;
        int o = 0;
        int u = 0;  
        while(!phrase.equals("quit")){
            for(int c = 0; c<length; c++){  
                char vowels = phrase.charAt(c);
                if(vowels == 'e'){
                    e++;
                }
                else if(vowels == "o"){
                    o++;
                }
            }
            System.out.println("There are " + a + "a's");
            System.out.println("There are " + e + "e's");
            System.out.println("There are " + i + "i's");
            System.out.println("There are " + o + "o's");
            System.out.println("There are " + u + "u's");
            System.out.println("enter another sentence");
            phrase = input.nextLine();
        }    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 2018-01-28
      • 2023-01-19
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多