程序接受单词作为i / p,检查单词辅音/元音中的每个单个字母。条件如果输入不是字母,则打印错误消息

问题描述 投票:0回答:1

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

继续输入'a'一次然后'after'。你会知道我真正想要的......

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 ");
                        }
                }
            }
        }
    }
}
java arrays loops character
1个回答
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!

另一种编码方式

这是另一种方法:

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

有关说明,请参阅内联注释:

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,你可以用lambda替换整个else

...
} else {
  inputString.toLowerCase().chars()
      .mapToObj(c -> vowelsList.contains((char) c) ? " VOWEL " : " consonant ")
      .forEach(System.out::print);
}
...
© www.soinside.com 2019 - 2024. All rights reserved.