2012-09-30 2 views
2

과제가 있습니다 ... 코드가 아래에 있습니다. 몇 가지 질문이 있습니다 ... 미리 감사드립니다. 이 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. 사용할 수있는 방법이 내 내가 필요없이 물어 모든 질문에 대한 문 IF 각 질문의 문자열 이름을 변경 하시겠습니까?
  2. 이러한 if 문에 대해 메서드 (?)를 만들 수있는 방법이 있습니까? 그래서 질문하는 모든 질문에 대해 작성할 필요가 없습니다.
  3. 사용자가 10 초 내에 답변을 입력하지 않으면 답변을 요청하는 타이머를 갖고 싶습니다. 어떻게해야합니까?
+0

@ 루이지 멘도자 - 좋은 일을 계속하십시오! ;) –

+0

@RichardJPLeGuen 할 수있을 때마다 나는 그것을하는 경향이 있습니다. –

+1

짧은 답변 : 1) 아니요, 2) 네,하지만 메서드는 모든'if' 문과'String' 문을 포함합니다. 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. 모두 Object으로 지정되는 wait() 방법을 조사하십시오.

제쳐두고, 사용자는 입력 한 내용으로 아무 것도하지 않습니다. input.nextLine()으로 전화하면 바닥에 떨어 뜨립니다. 변수를 캡처하는 방법을 살펴 봐야합니다.

+0

# 3의 경우, 그렇게한다고 생각하는 것을하지 않을 것입니다. 'input.nextLine()'은 블로킹 호출이며, 대기 중이다. 그는 이것을 위해 타이머를 원할 것입니다.이 타이머는 다른 코드와 함께 진행해야합니다. 또한 if 문을 switch 문으로 변경하거나 적어도 if-else if-else if (copy-pasted)를 변경해야합니다. – 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

나는이 일에 응답 여부를 모르겠어요,하지만이 대답한다 :

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));