我在寫這個簡單的程序,並在數據驗證部分,其中程序必須拒絕該用戶輸入,如果它包含包含一個數字,用ASCII文件,但使用(ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
似乎並沒有工作ASCII檢查輸入是否包含字母或數字不起作用?
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
public class Liu_YiJun_A2Q4 {
public static void main(String[] args) {
String fName = "", lName = "", type = "", add = "", post = "", use = "c", use2 = "";
int age = 0, phone = 0, numOfU = 0;
char ch = 0;
while (use.equalsIgnoreCase("c")) {
System.out.println(numOfU);
Scanner input = new Scanner(System.in);
System.out.print("Please enter your FIRST name: ");
fName = input.nextLine();
while (fName.length() >= 15 && (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
System.out.println("The characters in the name are too long OR The value is not a letter");
System.out.print("Please enter your FIRST name: ");
fName = input.nextLine();
}
System.out.print("Please enter your LAST name: ");
lName = input.nextLine();
System.out.print("Please enter your AGE: ");
age = input.nextInt();
input.nextLine();
System.out.print("Please either FT ot PT: ");
type = input.nextLine();
System.out.print("Please enter the ADDRESS: ");
add = input.nextLine();
System.out.print("Please enter the POSTAL CODE: ");
post = input.next();
System.out.print("Please enter your PHONE NUMBER: ");
phone = input.nextInt();
System.out.println();
System.out.println("*********************************************");
String val = "" + ((int) (Math.random() * 9000) + 1000);
System.out.println("Membership ID: " + val);
System.out.println("Name of new member: " + fName + " " + lName);
System.out.println("Age: " + age);
System.out.println("User is: " + type);
System.out.println("Address: " + add + " " + post);
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyy");
System.out.println("Member Since: " + sdf.format(cal.getTime()));
System.out.println();
System.out.print("Enter (C) to enter a new user or X to Exit: ");
use = input.next();
if (use.equals("x")) {
System.out.println("Thanks for using ICMSG Membership System. You have entered " + numOfU + " users in this session. Have a nice day!");
}
}
}
}
正如您從屏幕截圖中可以看到的,intelliJ突出顯示了「if」條件中的某些內容。將鼠標懸停在上面,看看它告訴你什麼。這或者意味着你的情況是錯誤的,總是_false_(我相信它),或者它總是正確的或者intelliJ表明你可以縮短條件。儘管如此,情況總是錯誤的。 –
請注意,您從未將0以外的任何內容分配給'ch'。 – luk2302
還應指定哪些其他內容? @ luk2302 –