用java编译程序判断输入的值是否为中文如果不是中文则提示输入错误
以下是使用Java编写的示例代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个字符串:");
String input = scanner.nextLine();
if (isChinese(input)) {
System.out.println("输入正确");
} else {
System.out.println("输入错误");
}
}
public static boolean isChinese(String str) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c < 0x4e00 || c > 0x9fa5) {
return false;
}
}
return true;
}
}
代码中使用了Scanner类来读取用户输入的字符串,然后调用isChinese方法来判断输入的值是否为中文。isChinese方法遍历输入的字符串,如果发现有任何一个字符不是中文,则返回false,表示输入错误。如果所有字符都是中文,则返回true,表示输入正确
原文地址: https://www.cveoy.top/t/topic/hd20 著作权归作者所有。请勿转载和采集!