【问题标题】:Program accept word as i/p, checks for each single character letter in word consonant/vowel. Condition Print error message if input is not letter程序接受单词作为 i/p,检查单词辅音/元音中的每个单个字符字母。条件 如果输入不是字母,则打印错误消息
【发布时间】:2018-03-06 07:18:21
【问题描述】:

输出应该只打印一次,但一旦我找到元音,就无法弄清楚它是否会脱离循环。

继续输入“a”一次,然后输入“aft”。你会知道我真正想要什么...

package com.java;

import java.util.Scanner;

public class three {

    @SuppressWarnings("resource")

    public static void main(String args[])
    {

        Scanner s = new Scanner(System.in);
        System.out.println("Enter String");
        String a = s.next();
        char b[] = a.toCharArray();
        ///char c[] = {"a","e","i","o","u"};
        String str = "aeiouAEIOU"; 
        char[] c = str.toCharArray();
        //char[] charArray = str.toCharArray();


        if(a.matches(".*\\d+.*"))
        {
            System.out.println("WARNING!!!");
            System.out.println("please input only string");
        }
        else
        {
            for(int i=0;i<b.length;i++)
            {
                for(int j=0;j<c.length;j++)
                {
                    if(b[i]==c[j])
                        {
                                System.out.print(" VOWEL ");
                        }
                    else if(b[i]!=c[i])
                        {
                            System.out.print(" consonant ");
                        }
                }
            }
        }
    }
}

【问题讨论】:

  • 你忘了问问题。
  • 输出不是按照程序应有的。对于输入=atr 输出应该是= VOWEL辅音辅音..
  • 什么是“i/p”?这不是输入,是吗?
  • 您的问题是,对于输入“a”,您测试“aeiouAEIOU”,但在找到 a 之后,您将其与“eiouAEIOU”进行比较,每个测试都失败了。元音彼此相似。
  • i/p 在这里被称为输入

标签: java arrays loops character


【解决方案1】:

问题

问题出在您的第二个 for 循环中,您不应该在其中打印元音或辅音。这个内部循环只是用来决定字符是否是元音,所以你应该在这个循环中更新一个布尔值,并根据布尔值的值在外部循环中打印出来。

代码已更正

这是您的代码更正(我更改了变量名称以便更容易理解):

public static void main(String args[]) {
  String vowels = "aeiouAEIOU";
  char[] vowelsArray = vowels.toCharArray();

  Scanner s = new Scanner(System.in);
  System.out.println("Enter String");
  String inputString = s.next();
  char inputStringArray[] = inputString.toCharArray();

  if(inputString.matches(".*\\d+.*")) {
    System.out.println("WARNING!!!");
    System.out.println("please input only string");
  } else {
    for(int i=0;i<inputStringArray.length;i++) {
      // Declare a boolean to say if the character is a Vowel or not
      boolean isVowel = false;
      // Check the character and set the boolean value
      for(int j=0;j<vowelsArray.length;j++) {
        if(inputStringArray[i]==vowelsArray[j]) {
          isVowel = true;
          break;
        }
      }
      // Then do the printing here, in the input characters loop
      if(isVowel) {
        System.out.print(" VOWEL ");
      } else if(inputStringArray[i]!=vowelsArray[i]) {
        System.out.print(" consonant ");
      }
    }
  }

关于正则表达式的注意事项

如果您只想接受字母,您可能更喜欢这个正则表达式。

if(!inputString.matches("[a-zA-Z]+"))

您当前的正则表达式将接受hey!

另一种编码方式

这是另一种方法:

  • 使用List 对象的contains 方法。
  • 将正则表达式测试修改为只接受字母
  • 输入字符串小写,因此我们的元音数组只能小写

解释见内联 cmets:

public static void main(String args[]) {

  // declare your vowels
  List<Character> vowelsList = Arrays.asList('a', 'e', 'i', 'o', 'u', 'y');

  // get the input string
  Scanner s = new Scanner(System.in);
  System.out.println("Enter String");
  String inputString = s.next();

  if(!inputString.matches("[a-zA-Z]+")) {
    System.out.println("WARNING!!!");
    System.out.println("please input only string");

  } else {
    // Transform your inputString to lower case 
    // (because we only have lower case in our vowels list)
    String lowerCaseInputString = inputString.toLowerCase();

    // Then for each character of the input string, 
    // check if it is in the vowels list or not
    for(char c : lowerCaseInputString.toCharArray()) {
      if(vowelsList.contains(c)) {
        System.out.print(" VOWEL ");
      } else {
        System.out.print(" consonant ");
      }
    }
  }
}

最后是 lambda 版本

如果您正在使用 Java 8 并且愿意使用 lambda,则可以将整个 else 块替换为 lambda

...
} else {
  inputString.toLowerCase().chars()
      .mapToObj(c -> vowelsList.contains((char) c) ? " VOWEL " : " consonant ")
      .forEach(System.out::print);
}
...

【讨论】:

  • 知道了!!你的第一个答案一切正常,但如果我使用第二个答案就不行了..虽然纠正了代码..竖起大拇指!!!
猜你喜欢
  • 2023-02-03
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
相关资源
最近更新 更多