我对编程很陌生,所以请原谅我的基本和有限的理解。基本上,我正在为学校项目创建记忆游戏。我想在for循环中做2个do-while循环,其工作原理如下:用户将被提示输入4个随机字母,这些字母将在第一个do-while循环中完成,第二个do-while循环将询问用户重新输入他们最初输入的短语。
所以我的第一个问题是,为什么只有第一个do-while执行?我假设for循环执行first-do-while而不是基于我的参数重复,因此第二个将永远不会执行但是,我很感激任何帮助理解原因,并且可能相应地重新格式化我的程序。
我的第二个问题是,我希望有一种分数计数器,以正确的顺序为每个正确猜测的字母为用户提供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());
}
}
}
答案 0 :(得分:0)
对于第二个问题,我不得不阅读String类文档。您所需要的只是迭代字符并相应地对复制结果作出反应。
在控制台中,要隐藏以前的用户输入,您可以清除屏幕,如:
Runtime.getRuntime().exex("cls);