简体   繁体   English

Java…循环程序

[英]Java…While Loop Program

I've got a homework assignment...code is below...got several questions about it...thanks in advance. 我已经完成了一项作业...下面的代码是...关于它的几个问题...在此先感谢。 Beginner Java student...if this looks cludgey, please don't laugh >.> Code below... Java初学者...如果看起来很笨拙,请不要笑>。>下面的代码...

    /*
     * Getting to know you...
     * @author Elle dela Victoria
     * @version 092812
     */
       import java.util.*;
       public class A15_1
       {
           public static void main(String[] args)
           {
           Scanner input = new Scanner(System.in);

           System.out.print(
           "Mind answering some questions for me?\n" +
           "Type quit when you're ready to leave.\n");

       while(true)
       {
        System.out.print("Does your name start with the letter H? ");
        input.nextLine();
        int ans = (int)(Math.random() * 5);
        if (ans == 0)
            System.out.println("That's awesome!");
        if (ans == 1)
            System.out.println("Awww, how unfortunate!");
        if (ans == 2)
            System.out.println("You're amazing at this!");
        if (ans == 3)
            System.out.println("LOL!");
        if (ans == 4)
            System.out.println("WTF!! That's horrible!");

        System.out.print("Are you male? ");
        input.nextLine();
        int ans1 = (int)(Math.random() * 5);
        if (ans1 == 0)
            System.out.println("That's awesome!");
        if (ans1 == 1)
            System.out.println("Awww, how unfortunate!");
        if (ans1 == 2)
            System.out.println("You're amazing at this!");
        if (ans1 == 3)
            System.out.println("LOL!");
        if (ans1 == 4)
            System.out.println("WTF!! That's horrible!");

        System.out.print("Are you female?");
        input.nextLine();
        int ans2 = (int)(Math.random() * 5);
        if (ans2 == 0)
            System.out.println("That's awesome!");
        if (ans2 == 1)
            System.out.println("Awww, how unfortunate!");
        if (ans2 == 2)
            System.out.println("You're amazing at this!");
        if (ans2 == 3)
            System.out.println("LOL!");
        if (ans2 == 4)
            System.out.println("WTF!! That's horrible!");

        System.out.print("Are you in school right now?");
        input.nextLine();
        int ans3 = (int)(Math.random() * 5);
        if (ans3 == 0)
            System.out.println("So angry when you're sober!");
        if (ans3 == 1)
            System.out.println("Awww, how unfortunate!");
        if (ans3 == 2)
            System.out.println("You're amazing at this!");
        if (ans3 == 3)
            System.out.println("LOL!");
        if (ans3 == 4)
            System.out.println("WTF!! That's horrible!");

        String userinput = input.nextLine();
        if (userinput.equalsIgnoreCase("quit"))
            break;      
      }
   }
 }
  1. is there any way to use my IF statements for every question I ask without having to change the String name for each question? 有什么方法可以对每个问题使用我的IF语句,而不必更改每个问题的字符串名称?
  2. is there any way to create a method (?) for those if statements, so I don't have to write them out for EVERY question I ask? 有什么方法可以为这些if语句创建方法(?),因此我不必为我提出的每个问题都将它们写出来?
  3. If the user doesn't input an answer in 10 seconds, I'd like to have a timer that prompts them for an answer, how do I do this? 如果用户在10秒钟内没有输入答案,我希望有一个计时器提示他们输入答案,我该怎么做?

You can have an array of strings and print them according to the input ans : 您可以拥有一个字符串数组,并根据输入ans打印它们:

    String str[] = {"That's awesome!", "Awww, how unfortunate!",
     "You're amazing at this!", "LOL!", "WTF!! That's horrible!"};

      /* code */

  while(true)
       {
        System.out.print("Does your name start with the letter H? ");
        input.nextLine();
        int ans = (int)(Math.random() * str.length);
        System.out.println(str[ans]);

      /* code */
   }
  1. Yes, you can. 是的你可以。 You create a method with a parameter of type int to use: 使用参数类型为int的方法创建要使用的方法:

     public void printReply(int ans) { if (ans == 0) System.out.println("So angry when you're sober!"); if (ans == 1) System.out.println("Awww, how unfortunate!"); if (ans == 2) System.out.println("You're amazing at this!"); if (ans == 3) System.out.println("LOL!"); if (ans == 4) System.out.println("WTF!! That's horrible!"); } 

    Call this where appropriate. 在适当的地方叫这个。

  2. Not sure I understand, I think the first answer covers this. 不确定我是否理解,我认为第一个答案涵盖了这一点。

  3. Look into the wait() method, which is specified by all Object s. 查看所有Object所指定的wait()方法。

As an aside, you're not doing anything with your input. 顺便说一句,您对输入不做任何事情。 You let it drop on the floor by calling input.nextLine() . 您可以通过调用input.nextLine()让它掉在地上。 You should look into capturing that in a variable. 您应该考虑将其捕获到变量中。

I'm not sure whether this answers 1, but it should answer 2: 我不确定这是否回答1,但应该回答2:

private String getAnswer() {
   int ans = (int)(Math.random() * 5);
   switch(ans) {
   case 0:
      return "That's awesome!";
   case 1:
      return "Awww, how unfortunate!";
   [... and so on ...]
   }
   return null
} 

Than just call it where you need an answer like this: System.out.println(getAnswer()); 比在需要这样的答案的地方调用它即可: System.out.println(getAnswer());

I would suggest using a list to store the responses. 我建议使用列表来存储响应。 This will alleviate the need for if statements and also prevents you from having to add a new response in four different places. 这将减轻对if语句的需要,并避免您不得不在四个不同的位置添加新的响应。

List<String> responseList = new ArrayList<String>();
responseList.add("That's awesome!");
responseList.add("LOL!");
responseList.add(.....);

System.out.println(responseList.get(ans));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM