@pablo 我似乎陷入了这个循环:
do{
System.out.println("Voulez-vous saisir d'autres parties? (o/n)");
reponse = niveau.nextLine(); // extraction de la dernière ligne
if (reponse.toLowerCase().equals("o") == false || reponse.toUpperCase().equals("O") == false || reponse.toLowerCase().equals("n") == false || reponse.toUpperCase().equals("N") == false) {
System.out.println("R\u00C9PONSE INVALIDE ! VEUILLEZ ENTRER O OU N.");
} else {
validationReponse = true;
}
}while(!validationReponse);
谢谢大家的帮助!我能够通过数字答案对问题进行验证,但现在我不知道如何使用“y或n”答案来解决问题。我尝试了这个,但它不起作用:
do{
System.out.println("Do you want to enter more game scores ? (y/n)");
reponse = niveau.nextLine();
if (reponse != o || reponse != n) {
System.out.println("Answer is invalid! Please enter y or n.");
} else {
validationReponse = true;
}
}while(!validationReponse);
我是初学者。我正在学习我的第一个java课程。在我的代码中,我想为问题添加一个验证:请选择一个游戏级别,你赢了游戏吗?你想要输入更多游戏吗,我需要确保输入是有效的,如果答案无效如果提供,它需要循环回到问题。我不知道该怎么做。 请不要在您的答案中使用高级编码。我是初学者班。它需要简单。
String reponse="O";
while (reponse.toLowerCase().equals("o"))
{
System.out.println("Please choose your game level :");
System.out.println (" ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println (" For Beginner press 1");
System.out.println (" For Advanced press 2");
System.out.println (" For Expert press 3");
System.out.println (" ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.print (" My choice is : ");
Niveau = niveau.nextInt();
if(Niveau == 1){
int choix1 = 0;
nbParties_Facile++;
System.out.println("Have you won the game ?");
System.out.println(" Press 1 for YES ");
System.out.println(" Press 2 for NO ");
choix1 = niveau.nextInt();
niveau.nextLine();
if (choix1 == 1){
nbPartiesFaciles_Finies++;
tauxReussite_Facile = nbPartiesFaciles_Finies / nbParties_Facile * 100;
System.out.println("How many times did you take to complete the grid ?");
System.out.println(" Please enter the time in minutes");
String afficheur5 = niveau.nextLine();
tempsResolution_Facile = (nbPartiesFaciles_Finies * tempsResolution_Facile) + Integer.parseInt(afficheur5) / (nbPartiesFaciles_Finies);
}
else if (choix1 == 2){
tauxReussite_Facile = nbPartiesFaciles_Finies - 1 / nbParties_Facile * 100;
}
else {
//default
}
}
if(Niveau == 2){
int choix2 = 0;
nbParties_Intermediaire++;
System.out.println("Have you won the game ?");
System.out.println(" Press 1 for YES ");
System.out.println(" Press 2 for NO ");
choix2 = niveau.nextInt();
niveau.nextLine();
if (choix2 == 1){
nbPartiesIntermediaires_Finies++;
tauxReussite_Intermediaire = nbPartiesIntermediaires_Finies / nbParties_Intermediaire * 100;
System.out.println("How many times did you take to complete the grid ?");
System.out.println(" Please enter the time in minutes");
String afficheur7 = niveau.nextLine();
tempsResolution_Intermediaire = (nbPartiesIntermediaires_Finies * tempsResolution_Intermediaire) + Integer.parseInt(afficheur7) / (nbPartiesIntermediaires_Finies);
}
else if (choix2 == 2){
tauxReussite_Intermediaire = nbPartiesIntermediaires_Finies - 1 / nbParties_Intermediaire * 100;
}
else {
//default
}
}
if(Niveau == 3){
int choix3 = 0;
nbParties_Expert++;
System.out.println("Have you won the game ?");
System.out.println(" Press 1 for YES ");
System.out.println(" Press 2 for NO ");
choix3 = niveau.nextInt();
niveau.nextLine();
if (choix3 == 1){
nbPartiesExpertes_Finies++;
tauxReussite_Expert = nbPartiesExpertes_Finies / nbParties_Expert * 100;
System.out.println("How many times did you take to complete the grid ?");
System.out.println(" Please enter the time in minutes");
String afficheur9 = niveau.nextLine();
tempsResolution_Expert = (nbPartiesExpertes_Finies * tempsResolution_Expert) + Integer.parseInt(afficheur9) / (nbPartiesExpertes_Finies);
}
else if (choix3 == 2){
tauxReussite_Expert = nbPartiesExpertes_Finies - 1 / nbParties_Expert * 100;
}
else {
//default
}
}
System.out.println("Do you want to enter more games ? (o/n)");
reponse = niveau.nextLine();
}
niveau.close();
答案 0 :(得分:0)
让我们以身份为例请选择游戏级别问题
所以你有很多方法可以做到这一点,我会尽量保持简单,因为你是语言的初学者,但目标始终是坚持面向对象的编程。但是,我们可以通过一种简单的方法来实现这一目标 。所以你可以有这样一个简单的方法:
private static boolean askForLevel() {
System.out.println("What is the level you want?");
String response = scanner.nextLine();
if (!response.equals("something")) {
return false;
}
askForLevel();
// More stuff
return true;
}
答案 1 :(得分:0)
如下所示,
do {
System.out.println("Please choose your game level :");
System.out.println (" ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println (" For Beginner press 1");
System.out.println (" For Advanced press 2");
System.out.println (" For Expert press 3");
System.out.println (" ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.print (" My choice is : ");
while (!sc.hasNextInt()) {
System.out.println("That's not a number, Please choose a Number !");
sc.next();
}
number = sc.nextInt();
} while (number<=3);
在if(number==1)
内做与上面相同的事情。
答案 2 :(得分:0)
我会使用JOptionPane而不是扫描仪...但这是一个意见问题。既然你是新手,你的老师可能不会直接进入面向对象的编程,这是好的,因为你需要理解基础知识,所以最少使用面向对象的编程:
//user input
int level = 0;
do{
level = Integer.parseInt(JOptionPane.showInputDialog("Choose the level :").trim());
}while(level < 1 || level > 3);
if (level == 1) {
//your code
}
else if(level == 2){
//your code
}
else if(level == 3){
//your code
}
你可以用像Pablo建议的私人方法来构建它。