2017-12-31 66 views
1

저는 프로그래밍에 익숙하지 않아서 기본적이고 제한적인 이해를 용서합니다. 본질적으로, 저는 학교 프로젝트를위한 메모리 게임을 만들고 있습니다. 나는 2 do-while 루프를 다음과 같이 작동하는 for 루프에서하고 싶다 : 사용자는 첫 번째 do-while 루프와 두 번째 do-while 루프에서 수행 할 4 개의 랜덤 문자를 입력하라는 메시지가 표시된다. 사용자가 처음에 입력 한 구를 다시 입력 할 수 있습니다.For 루프의 Java - 2-Do-While 루프

내 첫 번째 질문은 왜 첫 번째 do-while 만 실행합니까? 나는 for 루프가 first-do-while과 my parameters를 기반으로 반복을 수행한다고 가정하고있다. 그러므로 두 번째 것은 결코 실행되지 않을 것이다.

내 두 번째 질문은 정확한 순서로 올바르게 추측 된 모든 문자에 대해 사용자 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()); 
} 
} 
} 
+1

환영 SO에를! 이것을 여러 개의 작은 대상 질문으로 나누고 싶을 수도 있습니다. https://stackoverflow.com/help/how-to-ask – navicore

+0

을 참조하십시오. 문제는 'userLetters1.length() == stagePlayer'를 실행하자마자'stagePlayer = stagePlayer + 2;'를 실행하는 것입니다. 따라서 첫 번째 do-while 루프의 종료 조건이 충족되지 않습니다. – oneturkmen

+0

첫 번째 do-while 루프에서'if' 문에서'break' 할 필요가 있다고 생각합니다. – oneturkmen

답변

0

대신 디버그하십시오. 한 줄씩 어떤 일이 일어나는지 살펴보고 첫 번째 일을 끝내지 않는 이유를 알아보십시오. 나가면 두 번째 것이 실행되지 않는 이유가 없습니다.

두 번째 질문에서는 String 클래스 설명서를 읽으 려합니다. 캐릭터를 반복하고 copmarisions의 결과에 따라 반응하는 것만 있으면됩니다. 콘솔에서

, 당신은 같은 화면을 취소 할 수 이전 사용자의 입력을 숨기려면 :

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