[英]If a user inputs an invalid character how can I get my program to output a message to the user?
If the user enters the wrong character such as "E" or "A" in my program how can I output a message to the user? 如果用户在程序中输入了错误的字符,例如“ E”或“ A”,如何向用户输出消息? The line which I'm speaking about in my program is when the user has to choose to input one out of three characters P, M or D. However, if the user inputs another character which isn't listed I want the program to display a message.
我在程序中所说的那行是用户必须选择输入三个字符P,M或D中的一个。但是,如果用户输入了另一个未列出的字符,我希望程序显示一个消息。
As you can see if the user inputs "E" the program skips to the next step. 如您所见,如果用户输入“ E”,程序将跳至下一步。 However, I want the user to be notified and asked again to enter the correct value which is "P, M or D" .
但是,我希望通知用户并再次要求输入正确的值“ P,M或D” 。
In order to do what I'm trying to achieve I believe I need to do an if statement so here what I have tried. 为了做到我想要达到的目标,我相信我需要做一个if语句,所以在这里我已经尝试了。
if((i = P,M,D) && (i < ?)) {
System.out.println("ERROR, please input only P,M or D.");
}
Screenshot of the program 程序截图
JAVA CODE JAVA代码
String grade;
int yr2cred[] = new int[18];
char yr2grade[] = new char[18];
for (int i = 1; i <= 18; i++) {
System.out.println("Please enter your grade for unit " + i +" "+(username) + ".");
grade = userinput.next();
yr2grade[i - 1] = grade.charAt(0);
if (yr2grade[i - 1] == 'P' || yr2grade[i - 1] == 'p') {
yr2cred[i - 1] = yr2cred[i - 1] + P;
}
if (yr2grade[i - 1] == 'M' || yr2grade[i - 1] == 'm') {
yr2cred[i - 1] = yr2cred[i - 1] + M;
}
if (yr2grade[i - 1] == 'D' || yr2grade[i - 1] == 'd') {
yr2cred[i - 1] = yr2cred[i - 1] + D;
}
}
Possible code is here. 可能的代码在这里。 We iterate on units, for each unit we ask to enter the grade value until valid one will be entered.
我们以单位为单位进行迭代,对于每一个单位,我们要求输入等级值,直到输入有效的等级值为止。 At the end the list of grades is printed.
最后会列出成绩列表。
import java.util.Arrays;
import java.util.Scanner;
public class CheckInput {
public static void main(String[] args) {
int unitsCount = 18;
String[] unitGrades = new String[unitsCount];
String grade;
Scanner userInput = new Scanner(System.in);
for (int i = 0; i < unitsCount; i++) {
grade = "";
while ( ! (grade.equalsIgnoreCase("P") || grade.equalsIgnoreCase("M") || grade.equalsIgnoreCase("D") ) ) {
if (!"".equals(grade)){
System.out.println("ERROR, please input only P,M or D.");
}
System.out.print("Please enter your grade for unit " + i + ": ");
grade = userInput.next();
}
unitGrades[i] = grade;
}
System.out.println("Unit grades: "+Arrays.toString(unitGrades));
}
}
You can do this using a while loop within the for loop: 您可以在for循环中使用while循环进行此操作:
for (int i = 1; i <= 18; i++) {
while (grade.equals("")) {
System.out.println("Please enter your grade for unit " + i + " " + username + ".");
grade = userinput.next().toLowerCase();
yr2grade[i - 1] = grade.charAt(0);
if (yr2grade[i - 1] == 'p') {
yr2cred[i - 1] = yr2cred[i - 1] + P;
} else if (yr2grade[i - 1] == 'm') {
yr2cred[i - 1] = yr2cred[i - 1] + M;
} else if (yr2grade[i - 1] == 'd') {
yr2cred[i - 1] = yr2cred[i - 1] + D;
} else {
System.err.println("Invalid Grade Supplied! Try Again.\n");
grade = "";
}
}
grade = "";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.