2012-09-30 111 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));