简体   繁体   English

如果用户输入了无效字符,我如何使我的程序向用户输出消息?

[英]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.

相关问题 当用户输入无效的输入时,如何终止程序? - How can i terminate the program when the user inputs an invalid input? 如果用户输入特定数字,如何停止我的程序执行下一条语句? - How can I stop my program from executing the next statement if the user inputs a specific number? 我希望我的程序在用户输入除整数以外的任何内容并将其循环回时显示错误消息 - I want my program to display an error message when the user inputs anything other than an integer and loop it back 当用户输入小数点时,如何错误处理代码以防止程序崩溃? - How can I error handle my code to prevent my program from crashing when the user inputs a decimal point? 如何让程序能够获取用户输入的特殊字符的值 - How to make program can get the value of a special character that inputted by the user 用户在 Java 中输入密码错误 3 次后,如何让我的 Java 代码终止 - How can i get my java code to terminate after the user inputs the password wrong 3 times in Java 如何使用用户输入而不是无限循环计数我的while循环? - How Can I Get My While Loop To Count Using User Inputs and Not Infinitely Loop? 如何获得程序以将每个字符打印为“字符1 :(字符),字符2 :(字符)等”? - How can I get my program to print out each character as “Character #1: (character) , Character #2: (character), etc”? 程序正在运行,但当用户输入整数时出现错误 - Program is running but I get error when the user inputs integers 如何在不使用 replace 方法的情况下替换用户输入的字符串中的字符? - How can I replace a character in a string that the user inputs without using the replace method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM