【问题标题】:i need my program to stop a user from entering an inncorrect value. java我需要我的程序来阻止用户输入错误的值。爪哇
【发布时间】:2015-05-08 05:19:34
【问题描述】:

好的,我在字符中使用 switch 语句,如果用户将位置键入为“r”,则会出现消息并告诉他们选择正确的位置。一切正常,但是我希望代码不允许用户保存不正确的位置,直到输入正确的位置。

有人能读一下这段代码,也许可以告诉我它是如何完成的,谢谢。

    public Property(String theAddress, int theBedrooms, char theLocation)
{
        address = theAddress;
        bedrooms = theBedrooms;
        location = theLocation;
        switch(theLocation){
            case 'n': theLocation = 'N';

            case 'N': theLocation = 'N';
                break;
            case 'e': theLocation = 'E';
            case 'E': theLocation = 'E';
                break;
            case 's': theLocation = 'S';
            case 'S': theLocation = 'S';
                break;
            case 'w': theLocation = 'W';
            case 'W': theLocation = 'W';
                break;
            default: theLocation = '0'; 
                System.out.println("###########################");
                System.out.println("# Enter Correct Location  #");
                System.out.println("#    North London = N     #");
                System.out.println("#    East London = E      #");
                System.out.println("#    South London = S     #");
                System.out.println("#    West London = W      #");
                System.out.println("###########################");
         }


}

【问题讨论】:

  • 传统上,您使用的循环在输入正确的输入之前不会退出。
  • 使用 while(! isCorrectOne(myString)){myString =scanner.next()} 将其用于您所需的数据类型..
  • 验证用户输入是一个在很多地方都被广泛和反复讨论过的话题。请进行尽职调查,并在询问之前先自己进行研究和尝试。
  • 在对象构造函数中验证用户输入不是一个好主意,在用户输入值时验证它,如果仍然传递了错误值,则在构造函数中抛出异常,只是可以肯定。
  • 将 SO 用于标准帖子....首先尝试一些东西,如果遇到问题,敲门 SO .. :)

标签: java input


【解决方案1】:

您可以迭代获取输入,直到获得正确的输入:

takeInput:  // It is Label <---------------------
while(true){                                    |
   //Take Input Here                            |
switch(theLocation){                            |
        case 'n': theLocation = 'N';            |
                                                |
case 'N': theLocation = 'N';                    |
        break;                                  |
        case 'e': theLocation = 'E';            |
        case 'E': theLocation = 'E';            |
        break;                                  |
        case 's': theLocation = 'S';            |
        case 'S': theLocation = 'S';            |
        break;                                  |
        case 'w': theLocation = 'W';            |
        case 'W': theLocation = 'W';            |
        break;                                  |
   default: continue takeInput; //Jump to label |
    }
}

当你得到正确的结果时打破循环

【讨论】:

    【解决方案2】:

    在您使用 while 循环获取用户输入的方法中。比如

    private void takeInput(){
        boolean isValid = false;
    
        while(!isValid){
            //Ask for input
            if(input = 'e' || input = 's'){
                isValid = true;
            }
            else{
                System.out.println("Invalid input");
            }
            //if input isn't valid loop will be entered again
            //and user can be asked for input again
        }
    
        //Continue with code to save here 
    }
    

    上面的代码可以工作,但是你会在 if 语句中有一些很好的条件。就我个人而言,我会做一些更好的事情。

    public void takeInput(){
        boolean isValid = false;
    
        while(!isValid){
            //Ask for input
            if(checkIsValid(input)){
                //set isValid to true to exit loop
                isValid = true;
                //Set the location to the inputed character, make sure its upper case
                theLocation = Character.toUpperCase(input);
            }
            else{
                System.out.println("Invalid input");
            }
            //if input isn't valid loop will be entered again
            //and user can be asked for input again
        }
        //You could then set 
        //Continue with code to save here 
    }
    
    private boolean checkIsValid(char c){
        switch(c){
            case 'n':
            case 'N':
            case 'e':
            case 'E':
            case 'S':
            case 's':
            case 'w':
            case 'W':
                return true;
            break;
    
            default:
                return false:
            break;  
        }
    }
    

    这是一种更简洁的解决方案,可以帮助您的代码变得更具可读性。它将接受用户输入,调用一个辅助方法来检查输入是否有效。如果输入有效,它会将位置保存到 theLocation 变量(大写)并退出循环。如果它无效,它将再次循环。

    【讨论】:

      【解决方案3】:

      想简单... 使用while循环并在正确的位置使条件为假

      boolean isCorrect = false;
      while(!isCorrect){
      //your switch case here
      //make isCorrect = true in cases (E,S and W)
      }
      

      我的意思是为您的情况(E、S 和 W)设置 isCorrect = true 当 isCorrect 变量的值为 true 时。 while 循环中断,你就完成了:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-25
        相关资源
        最近更新 更多