基本 Java:基本测验的 While 循环? [英] Basic Java: While loop for basic quiz?

查看:41
本文介绍了基本 Java:基本测验的 While 循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨晚开始学习 Java,并且我正在尝试编写我的第一个代码,而不是由我口述!

I started learning Java last night, and I'm trying to make my first code without it being dictated to me!

这是一个简单的测验问题,询问用户你认为我的狗可爱吗?"如果他们回答是",狗就会汪汪地微笑.如果他们说不",他会对他们咆哮并皱眉.如果给出不同的答案,则会打印什么?请再试一次".

It is a simple quiz question that asks the user "Do you think my dog is cute?" If they answer "Yes," the dog Woofs and smiles. If they say "no" He growls at them and frowns. If a different answer is given, "What? Please try again" is printed.

到目前为止,我对此没有任何问题,但现在我正在尝试这样做,以便在输入无法识别的答案时,再次提出问题,并重复此问题,直到他们回答是"或否"."这是我尝试过的:

I have no issue getting this working so far, but now I am trying to make it so that when an unrecognised answer is put in, the question is asked again, and this repeats until they answer either "Yes" or "No." Here is what I have tried:

import java.util.Scanner;
public class CopyOfMain {
    public static void main(String joel[]){
        System.out.println("Do you think my dog Olly is cute? Yes or No");
        Scanner ans = new Scanner(System.in);
        String Ans = ans.nextLine();
        while (!Ans.equals("Yes") || !Ans.equals("No")); {
            if (Ans.equals("Yes")){
                System.out.print("Woof :-)");

            }else if(Ans.equals("No")){
                System.out.print("Grrrr :-(");

            }else{  
                System.out.print("What? Please try again.");
            }   
        }
    ans.close();
    }
}

我在工作代码中添加的只是 while 循环,但现在如果输入是"或否",则不会出现打印的低音或咆哮声.但是,如果输入了是"或否"以外的其他内容,则会打印出正确的单词并打印出来,但仍然不会重新提出问题.有人可以建议修复吗?

All I have added to the working code is the while loop, but now if "Yes" or "No" is entered, there is no printed woof or growl. However, if something other than "Yes" or "No" is entered, the correct words and printed, but the question is still not re-asked. Could someone please suggest a fix?

推荐答案

重新读取循环中的行(也使用 AND not an OR nothing is both "Yes" and "No" or 一个更简单的无限循环和中断),还有 - 我会使用另一个变量 Ansans 对我的口味来说太相似了,另外,请注意,我正在测试你的独立于大小写的字符串(我认为是"和是"也应该有效).最后,在 while 之后和开括号之前没有分号(这会产生一个空循环) -

Re-read the line in the loop (also use an AND not an OR nothing is both "Yes" and "No" or an the simpler infinite loop and break), also - I would use another variable Ans and ans are too similar for my tastes, Additionally, note that I'm testing your strings independent of case (I assume "yes" and "YES" should also work). Finally, no semicolon after while and before the open bracket (that makes an empty loop) -

String response = ans.nextLine();
while (true) {
  if (response.equalsIgnoreCase("Yes")){
    System.out.println("Woof :-)");
    break;
  }else if(response.equalsIgnoreCase("No")){
    System.out.println("Grrrr :-(");
    break;
  }else{  
    System.out.println("What? Please try again.");
    response = ans.nextLine();
  }   
}

这篇关于基本 Java:基本测验的 While 循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆