简体   繁体   English

Java-For循环中的2-Do-While循环

[英]Java - 2-Do-While Loops in a For Loop

I'm quite new to programming so excuse my basic and limited understanding. 我是编程的新手,请原谅我有限的理解。 Essentially, I am creating a Memory Game for a school project. 本质上,我正在为一个学校项目创建一个记忆游戏。 I want to do 2 do-while loops in a for loop that works like this: the user will be prompted to enter a 4 random letters which will be done in the first do-while loop and the second do-while loop will ask the user to re-input the phrase that they had initially entered. 我想在一个像这样工作的for循环中执行2个do-while循环:将提示用户输入4个随机字母,这将在第一个do-while循环中完成,第二个do-while循环将要求用户重新输入他们最初输入的短语。

So my first question is, why does only the first do-while execute? 所以我的第一个问题是,为什么只执行第一个do-while? I'm assuming that the for loop executes the first-do-while and than repeats based on my parameters therefore the second one will never execute but, I'd appreciate any help understanding why, and reformatting my program accordingly perhaps. 我假设for循环执行第一次操作,然后根据我的参数重复执行,因此第二个将永远不会执行,但是,我很感谢理解原因的任何帮助,并可能重新格式化了程序。

My second question is that, I want to have a sort of score counter that nets the user 10 points for every correctly guessed letter in the correct sequence and deduct 10 for every incorrect character in the wrong sequence. 我的第二个问题是,我想拥有一种计分计数器,可以按正确顺序为每个正确猜出的字母给用户10分,为错误顺序中的每个不正确字符扣除10分。 How would I go about doing so, and what may I have to utilize to make this possible? 我将如何去做,我必须利用什么来使之成为可能?

Lastly, I would appreciate if anyone could point towards a way of concealing the letters that the user inputs. 最后,如果有人能指出隐藏用户输入字母的方法,我将不胜感激。

Any other suggestions to make my program more efficient would be greatly appreciated! 任何其他使我的程序更有效的建议将不胜感激!

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());
}
}
}

Don't assume. 不要假设 Debug instead. 改为调试。 Look what happens line by line and determine the reason the first do lop never exits. 逐行查看发生的情况,并确定第一次失败永远不会退出的原因。 It if exited I see no reason why the second one didn't execute. 如果退出了,我看不出为什么第二个没有执行。

For second question I'd enciurage to read String class documentation. 对于第二个问题,我会鼓励阅读String类文档。 All you need is to iterate through characters and react accordingly to results of copmarisions. 您所需要做的就是遍历字符,并对copmarisions的结果做出相应的反应。

In console, to hide previous user input you could clear the screen like: 在控制台中,要隐藏以前的用户输入,可以清除屏幕,例如:

Runtime.getRuntime().exex("cls);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM