英文:
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 if
s: 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");
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论