是否有一种方法可以从用户那里输入一个特定的字符,如果没有输入这个特定的字符,就会出现错误



我目前正试图让用户输入一个字符来说明他们是L为本地还是I为国际。通过让它们输入一个特定的字符但是我遇到的问题是对于字符输入我可以输入不止一个字符例如一个字符串或一个整数而它应该只是一个字符

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
double electrical_e_cost = 850.53;
double mechanical_eng_cost =895.25;
double petroleum_eng_cost = 450.67;
double computer_eng_cost = 921.14;   
int studentcount = 10;
int idnumber []=new int[10];
String[] studentname = new String[2];
int courseamount[]=new int [10];
char stype []=new char[10];
char coursetype []=new char[10];



for(int i=0; i<idnumber.length;i++)
{  

System.out.println("Enter Name ");
studentname[i]=input.next();

System.out.println("Please enter Student ID number ");
idnumber[i]= input.nextInt();

System.out.print("Please say wheather you are an internationanal or Local student by typing I or L respectively");
char t=input.next().charAt(0);

System.out.println("Please enter the course type either E,M,P,C ");
coursetype[i] =input.next().charAt(0);

System.out.println("Please enter The course amount  ");
courseamount[i]= input.nextInt();




} 
}

}

验证自己的输入:

System.out.print("Please say wheather you are an internationanal or Local student by typing I or L respectively");
char t=input.next().charAt(0);
while(!t.equals("L") && !t.equals("I")) {
System.out.print("Please say wheather you are an internationanal or Local student by typing I or L respectively");
t=input.next().charAt(0);
}

相关内容

  • 没有找到相关文章

最新更新