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

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

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

问题

import java.util.*;

public class MessAround {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("请输入字母表中的单个字符:");
        String letter = scan.next();

        if (letter.length() == 1 && Character.isLetter(letter.charAt(0))) {
            letter = letter.toLowerCase();
            if (letter.equals("a") || letter.equals("e") || letter.equals("i") || letter.equals("o") || letter.equals("u")) {
                System.out.println("元音");
            } else {
                System.out.println("辅音");
            }
        } else {
            System.out.println("错误");
        }
    }
}
英文:

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?

import java.util.*;

public class MessAround {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Please Provide single character from the alphabet: ");
        String letter = scan.next();
        
        if (scan.hasNext())
        {
            letter = letter.toLowerCase();
            if (letter.equals("a") || letter.equals("e") || letter.equals("i") || letter.equals("o") || letter.equals("u"))
            {
                System.out.println("Vowel");
            }
            else if (letter.length() > 1)
            {
                System.out.println("Error");
            }
            else
            {
                System.out.println("Consonant");
            }
        }
        else
        {
            System.out.println("Error");
        }
	}

}

答案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

使用正则表达式:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.print("请输入字母表中的单个字符:");
        String letter = scan.next();

        if (!letter.matches("[A-Za-z]")) {
            System.out.println("错误");
        } else {
            letter = letter.toLowerCase();
            if (letter.matches("[aeiou]")) {
                System.out.println("元音");
            } else {
                System.out.println("辅音");
            }
        }
    }
}

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

使用Character类:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.print("请输入字母表中的单个字符:");
        String letter = scan.next();

        if (letter.length() == 1 && Character.isAlphabetic(letter.charAt(0))) {
            char ch = Character.toLowerCase(letter.charAt(0));
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                System.out.println("元音");
            } else {
                System.out.println("辅音");
            }
        } else {
            System.out.println("错误");
        }
    }
}

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

英文:

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

Using Regex:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		System.out.print("Please Provide single character from the alphabet: ");
		String letter = scan.next();

		if (!letter.matches("[A-Za-z]")) {
			System.out.println("Error");
		} else {
			letter = letter.toLowerCase();
			if (letter.matches("[aeiou]")) {
				System.out.println("Vowel");
			} else {
				System.out.println("Consonant");
			}
		}
	}
}

Learn about regex from here.

Using the class, Character:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		System.out.print("Please Provide single character from the alphabet: ");
		String letter = scan.next();

		if (letter.length() == 1 && Character.isAlphabetic(letter.charAt(0))) {
			char ch = Character.toLowerCase(letter.charAt(0));
			if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
				System.out.println("Vowel");
			} else {
				System.out.println("Consonant");
			}
		} else {
			System.out.println("Error");
		}
	}
}

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:

确定