2012-09-30 79 views
2

我有一个家庭作业...代码是下面...有几个关于它的问题...提前感谢。初学Java的学生......如果这看起来cludgey,请不要笑>>下面 代码...Java ... while循环程序

/* 
    * 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. 有没有办法用我的,若对所有的问题,我问,而无需声明更改每个问题的字符串名称?
  2. 有没有办法为那些if语句创建一个方法(?),所以我不必为每个问题都写出来?
  3. 如果用户在10秒内没有输入答案,我想要一个计时器来提示他们答案,我该怎么做?
+0

@Luiggi门多萨 - 保持良好的工作! ;) –

+0

@RichardJPLeGuen我倾向于每次都能做到。 –

+1

简答题:1)不,2)是的,但该方法将包含所有if语句和其中的字符串。 3)这是可能的,但你说你是初学者,所以最好不要在控制台应用程序中查找此功能。 –

回答

0
  1. 是的,你可以。您创建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!"); 
    } 
    

    调用此酌情。

  2. 不知道我明白了,我想第一个答案涵盖了这个。

  3. 查看wait()方法,该方法由所有Object s指定。

顺便说一句,你没有对你的输入做任何事情。你可以致电input.nextLine()让它落在地板上。你应该考虑在变量中捕获它。

+0

对于#3,这不会做你认为它会做的事。 'input.nextLine()'是一个阻塞调用,原样是'wait'。他会想要一个定时器,这需要一些其他的代码。你还应该把'if's改成switch语句,或者至少'if-else if-else if'(我知道它是复制粘贴的)。 – pickypg

1

你可以有一个字符串数组,并打印出来,根据输入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 */ 
    } 
0

我不知道这是否回答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 
} 

比只是在你需要这样的答案的地方叫:System.out.println(getAnswer());

0

我会建议使用列表来存储响应。这将减轻对if语句的需求,并且还可以防止您必须在四个不同的地方添加新的响应。

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

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