[英]Java - 2-Do-While Loops in a For Loop
我是编程的新手,请原谅我有限的理解。 本质上,我正在为一个学校项目创建一个记忆游戏。 我想在一个像这样工作的for循环中执行2个do-while循环:将提示用户输入4个随机字母,这将在第一个do-while循环中完成,第二个do-while循环将要求用户重新输入他们最初输入的短语。
所以我的第一个问题是,为什么只执行第一个do-while? 我假设for循环执行第一次操作,然后根据我的参数重复执行,因此第二个将永远不会执行,但是,我很感谢理解原因的任何帮助,并可能重新格式化了程序。
我的第二个问题是,我想拥有一种计分计数器,可以按正确顺序为每个正确猜出的字母给用户10分,为错误顺序中的每个不正确字符扣除10分。 我将如何去做,我必须利用什么来使之成为可能?
最后,如果有人能指出隐藏用户输入字母的方法,我将不胜感激。
任何其他使我的程序更有效的建议将不胜感激!
import java.util.Scanner;
import java.lang.String;
public class MemoryGame {
public static void main(String[]args){
Scanner input = new Scanner(System.in);
int choice;
System.out.println("This is a M E M O R Y G A M E");
System.out.println("Press '1' for instructions");
System.out.println("Press '2' to play");
choice = input.nextInt(); //Checks user selection and redirects
if (choice == 1) {
Instructions();
} else {
playGame();
}
input.close();
}
public static void Instructions() { //Instructions method
int choice;
Scanner input = new Scanner(System.in);
System.out.println("This is a memory game. Below are a few instructions on how to play as well as few hints, and tricks");
System.out.println("> Players wlll be given a input box to input a given number of letters or numbers depending on the stage level.");
System.out.println("> To progress and gain points, users must sucessfully recall the set phrase that they have inputted.");
System.out.println("> Based on the number of correct letters, users will gain points and progress through a set of levels that increase in difficulty.");
System.out.println("> Upon making 3 incorrect character selections users will be displayed a 'Game Over' screen from which they may:");
System.out.println("1. Head to the main menu");
System.out.println("2. View the instructions");
System.out.println("3. Play again");
System.out.println("If users successfully oomplete 5 stages with no errors, they will be prompted a challenge level in which more characters will be required");
System.out.println(" ");
System.out.println("1. Press '1' to return to play");
choice = input.nextInt();
if (choice == 1) {
playGame();
}
input.close(); //Closes input.
}
public static void playGame() {
int userNumbers1;
int userNumbers2;
String userLetters1;
String userLetters2;
int scorePlayer;
int livesPlayer = 3;
int stagePlayer = 4;
int stageGeneral = 1;
Scanner input = new Scanner(System.in);
System.out.println("This is the M E M O R Y G A M E");
System.out.println("Stage 1 initializing . . .");
System.out.println("Please enter " + stagePlayer + " letters of your choice.");
for (int i = 0; i <= 10; i++) {
do {
userLetters1 = input.nextLine();
userLetters1 = userLetters1.toLowerCase(); userLetters1.trim();
if (userLetters1.length()==stagePlayer) {
System.out.println (". . .!");
stagePlayer = stagePlayer + 2;
stageGeneral = stageGeneral + 1;
} else {
System.out.println("Please enter " + stagePlayer + " letters");
}
}
while ( userLetters1.length() != stagePlayer);
do {
userLetters2 = input.nextLine();
userLetters2 = userLetters2.toLowerCase(); userLetters2.trim();
if (userLetters2.length()==userLetters1.length() && userLetters2.equals (userLetters1)) {
System.out.println (". . .");
System.out.println ("Great job!");
System.out.println("Stage " + stageGeneral + " initializing . . .");
System.out.println("Please enter " + stagePlayer + " letters of your choice.");
} else {
System.out.println ("Please enter " + userLetters1.length() + "letters that were previously entered.");
}
}
while ( userLetters1.length() != userLetters2.length());
}
}
}
不要假设 改为调试。 逐行查看发生的情况,并确定第一次失败永远不会退出的原因。 如果退出了,我看不出为什么第二个没有执行。
对于第二个问题,我会鼓励阅读String类文档。 您所需要做的就是遍历字符,并对copmarisions的结果做出相应的反应。
在控制台中,要隐藏以前的用户输入,可以清除屏幕,例如:
Runtime.getRuntime().exex("cls);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.