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;
}
}
}
- 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?
- 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?
- 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?
@Luiggi Mendoza - Continuez votre bon travail! ;) –
@RichardJPLeGuen J'ai tendance à le faire chaque fois que je le peux. –
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. –