【问题标题】:With hasNextInput() in a while loop it gives the loop for every String given but I want it to prompt once在 while 循环中使用 hasNextInput() 它为给定的每个 String 提供循环,但我希望它提示一次
【发布时间】:2023-04-01 20:35:01
【问题描述】:
    public void choice(){

    this.damageCalculator();
    story.typeWriterEffect("Give your choice of attack: \n");

            while (!input.hasNextInt()){
                story.typeWriterEffect("Please enter the correct number (1, 2 or 3).(ERROR NO INT)\n");
                story.typeWriterEffect("Give your choice of attack: \n");
                input.reset();
                input.next();
            }
            setUserChoiceOfAttack(input.nextInt());

        if(getUserChoiceOfAttack() > 0 && getUserChoiceOfAttack() < 4){
            this.userAttackMoment();
        } else {
            story.typeWriterEffect("Please enter the correct number (1, 2 of 3).(ERROR INPUT INVALID)\n");
            this.choice();
            }
}

问题: 如果您在输入中输入“Lol nope”,那么它将输出“请输入...”和“给出您的选择...”两次(对于键入的每个单词并用空格分隔。 一切运行良好,但困扰我的一件事是,如果用户输入了他不应该输入的内容,我只希望它重复一次(即使它是由空格分隔的多个字符串)。

感谢初学者在这里和任何关于我的代码编写的反馈!

编辑:

pivu0 的评论成功了! 这是我的固定代码,完美运行!

public void choice(){

    this.damageCalculator();

        story.typeWriterEffect("Give your choice of attack: \n");

        boolean loopCondition = true;
        while(loopCondition == true){

            try{                    
                player.setPlayerChoiceOfAttack(Integer.parseInt(input.nextLine()));

                if(player.getPlayerChoiceOfAttack() > 0 && player.getPlayerChoiceOfAttack() < 4){
                    loopCondition = false;
                } else {
                    story.typeWriterEffect("Please enter the correct number (1, 2 of 3).(ERROR INPUT INVALID)\n");
                    }
            }
            catch (NumberFormatException e){
                loopCondition = true;
                story.typeWriterEffect("Please enter the correct number (1, 2 or 3).(ERROR NO INT)\n");
                story.typeWriterEffect("Give your choice of attack: \n");
            }
        }
        this.userAttackMoment();
}

【问题讨论】:

    标签: string input while-loop java.util.scanner


    【解决方案1】:

    有一个布尔变量来检查尝试

    //attempt is initally false
    public void choice(boolean attempt){
    
        this.damageCalculator();
    
            story.typeWriterEffect("Give your choice of attack: \n");
    
                while (!input.hasNextInt()){
                    story.typeWriterEffect("Please enter the correct number (1, 2 or 3).(ERROR NO INT)\n");
                    story.typeWriterEffect("Give your choice of attack: \n");
                    input.reset();
                    input.next();
                }
                    setUserChoiceOfAttack(input.nextInt());
    
            if(getUserChoiceOfAttack() > 0 && getUserChoiceOfAttack() < 4){
                this.userAttackMoment();
            } else if(!attempt){
                story.typeWriterEffect("Please enter the correct number (1, 2 of 3).(ERROR INPUT INVALID)\n");
                this.choice(true);
                }
    }
    

    【讨论】:

      【解决方案2】:

      根据您使用的方法,我假设输入是Scanner 的一个实例。

      如果你阅读了hasNextInthttps://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt()的文档

      您会看到它检查下一个 TOKEN 是否为 int。对于扫描仪类,令牌由空格分隔。所以它会检查你的第一个词,断定'lol'不是一个Int,遍历循环,断定'nop'不是一个Int,再次遍历循环......

      尝试将整个扫描仪输入作为字符串 input.nextLine() 并尝试使用 Integer.parseInt 检查此字符串是否为整数

      如果 input.nextLine() 是一个整数,它将跳出循环。如果不是,则检查新输入:

      int number=0;
      boolean loopCondition = false;
      while(!loopCondition){
       try {
          number = Integer.parseInt(input.nextLine())
          loopCondition = true;
          } 
       catch (NumberFormatException e){
          story.typeWriterEffect("Please enter the correct number (1, 2 or 3).(ERROR NO INT)\n");
                  story.typeWriterEffect("Give your choice of attack: \n");
          loopCondition = false;
       }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-27
        • 2022-11-16
        • 2014-01-21
        • 1970-01-01
        • 1970-01-01
        • 2017-07-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多