Java 编码帮助!无法使 if 语句确认字符串

huangapple go评论104阅读模式
英文:

Java Coding Help! Can't get if statement to confirm string

问题

  1. import java.util.*;
  2. public class MessAround {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. System.out.println("请输入字母表中的单个字符:");
  6. String letter = scan.next();
  7. if (letter.length() == 1 && Character.isLetter(letter.charAt(0))) {
  8. letter = letter.toLowerCase();
  9. if (letter.equals("a") || letter.equals("e") || letter.equals("i") || letter.equals("o") || letter.equals("u")) {
  10. System.out.println("元音");
  11. } else {
  12. System.out.println("辅音");
  13. }
  14. } else {
  15. System.out.println("错误");
  16. }
  17. }
  18. }
英文:

Write a program that prompts the user to provide a single character from the alphabet. Print Vowel or Consonant, depending on the user input. If the user input is not a letter (between a and z or A and Z), or is a string of length > 1, print an error message.

I have been trying to solve this problem, and basically have it all figured out but have ran into two issues. How do I get the scanner to recognize an error if the user inputs a number rather than a letter. Also how would one not have to repeat their input. Is there a way to have the first if statement evaluate if the user entered a string?

  1. import java.util.*;
  2. public class MessAround {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. System.out.println("Please Provide single character from the alphabet: ");
  6. String letter = scan.next();
  7. if (scan.hasNext())
  8. {
  9. letter = letter.toLowerCase();
  10. if (letter.equals("a") || letter.equals("e") || letter.equals("i") || letter.equals("o") || letter.equals("u"))
  11. {
  12. System.out.println("Vowel");
  13. }
  14. else if (letter.length() > 1)
  15. {
  16. System.out.println("Error");
  17. }
  18. else
  19. {
  20. System.out.println("Consonant");
  21. }
  22. }
  23. else
  24. {
  25. System.out.println("Error");
  26. }
  27. }
  28. }

答案1

得分: 0

将以下内容添加到您的if语句中:if (isNumber(letter)) { // 输入了一个数字。 并创建一个名为 isNumber(String) 的方法,如果字符串在您所指的意义上是一个数字,则返回 true

或者更好地将其制作成一个名为 String isValid(String) 的方法,并在方法中实现您的验证规则。如果验证成功,它可以返回 null,否则返回一条消息。

英文:

Add to your ifs: if (isNumber(letter)) { // A number was entered. and create a isNumber(String) method that returns true if the string is a number in the sense that you mean it.

Or better make it a String isValid(String) method and implement in the method your validation rules. It could return null if the validation is successful otherwise a message.

答案2

得分: 0

使用正则表达式:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. System.out.print("请输入字母表中的单个字符:");
  6. String letter = scan.next();
  7. if (!letter.matches("[A-Za-z]")) {
  8. System.out.println("错误");
  9. } else {
  10. letter = letter.toLowerCase();
  11. if (letter.matches("[aeiou]")) {
  12. System.out.println("元音");
  13. } else {
  14. System.out.println("辅音");
  15. }
  16. }
  17. }
  18. }

这里了解有关正则表达式的内容。

使用Character类:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. System.out.print("请输入字母表中的单个字符:");
  6. String letter = scan.next();
  7. if (letter.length() == 1 && Character.isAlphabetic(letter.charAt(0))) {
  8. char ch = Character.toLowerCase(letter.charAt(0));
  9. if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
  10. System.out.println("元音");
  11. } else {
  12. System.out.println("辅音");
  13. }
  14. } else {
  15. System.out.println("错误");
  16. }
  17. }
  18. }

这里了解有关Character类的信息。

英文:

There are many ways to solve this. A couple of them are as follows:

Using Regex:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. System.out.print("Please Provide single character from the alphabet: ");
  6. String letter = scan.next();
  7. if (!letter.matches("[A-Za-z]")) {
  8. System.out.println("Error");
  9. } else {
  10. letter = letter.toLowerCase();
  11. if (letter.matches("[aeiou]")) {
  12. System.out.println("Vowel");
  13. } else {
  14. System.out.println("Consonant");
  15. }
  16. }
  17. }
  18. }

Learn about regex from here.

Using the class, Character:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. System.out.print("Please Provide single character from the alphabet: ");
  6. String letter = scan.next();
  7. if (letter.length() == 1 && Character.isAlphabetic(letter.charAt(0))) {
  8. char ch = Character.toLowerCase(letter.charAt(0));
  9. if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
  10. System.out.println("Vowel");
  11. } else {
  12. System.out.println("Consonant");
  13. }
  14. } else {
  15. System.out.println("Error");
  16. }
  17. }
  18. }

huangapple
  • 本文由 发表于 2020年9月30日 02:32:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64125606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定