2012-09-30 1 views
2

J'ai un devoir ... le code est ci-dessous ... J'ai plusieurs questions à ce sujet ... merci d'avance. Débutant Java étudiant ... si cela ressemble cludgey, s'il vous plaît ne riez pas>.> code ci-dessous ...Java ... While Loop Program

/* 
    * 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. est-il possible d'utiliser mon instructions IF pour chaque question que je pose sans avoir changer le nom de la chaîne pour chaque question?
  2. est-il possible de créer une méthode (?) Pour ces instructions if, donc je n'ai pas besoin de les écrire pour CHAQUE question que je pose?
  3. Si l'utilisateur ne saisit pas de réponse dans les 10 secondes, je voudrais avoir une minuterie qui les invite à répondre, comment faire?
+0

@Luiggi Mendoza - Continuez votre bon travail! ;) –

+0

@RichardJPLeGuen J'ai tendance à le faire chaque fois que je le peux. –

+1

Réponses courtes: 1) non, 2) oui, mais la méthode inclura toutes vos instructions 'if's et les' String's dedans. 3) C'est possible mais vous dites que vous êtes un débutant, donc il vaudrait mieux ne pas chercher cette fonctionnalité dans les applications console. –

Répondre

0
  1. Oui, vous pouvez. Vous créez une méthode avec un paramètre de type int à utiliser:

    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!"); 
    } 
    

    Appelez ce, le cas échéant.

  2. Je ne suis pas sûr de comprendre, je pense que la première réponse couvre cela. Regardez dans la méthode wait(), qui est spécifiée par tous Object s.

En passant, vous ne faites rien avec votre entrée. Vous le laissez tomber sur le sol en appelant input.nextLine(). Vous devriez chercher à capturer cela dans une variable.

+0

Pour le numéro 3, cela ne va pas faire ce que vous pensez qu'il va faire. 'input.nextLine()' est un appel bloquant, tel que 'wait'. Il voudrait une minuterie pour cela, ce qui nécessitera un autre code pour l'accompagner. Vous devriez aussi changer le 'if's en une instruction switch, ou au moins' if-else if-else if' (je réalise qu'il a été copié-collé). – pickypg

1

Vous pouvez avoir un tableau de chaînes et les imprimer selon l'entrée 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

Je ne suis pas sûr que cela répond à 1, mais il devrait répondre à 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 
} 

Il suffit de l'appeler où vous avez besoin d'une réponse comme ceci: System.out.println(getAnswer());

0

Je suggère d'utiliser une liste pour stocker les réponses. Cela allégera le besoin d'instructions if et vous évite également d'avoir à ajouter une nouvelle réponse à quatre endroits différents.

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

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