studio programacion móviles initcomponents error desarrollo curso aplicaciones java while-loop break

java - móviles - manual de programacion android pdf



No se puede detener mientras está en bucle (3)

Con bucles anidados si desea break el bucle externo, puede agregar un prefijo de etiqueta 1 .

game: while (true) {

entonces podrías

break game;

para terminar la declaración que está etiquetada game .

1 Ver también JLS-14.7. Las declaraciones etiquetadas que dicen (en parte) declaraciones pueden tener prefijos de etiqueta .

En este juego que creé, encontré un problema. Todo funciona muy bien, excepto que si fallas por alguna razón, el juego simplemente se reinicia y eso no es lo que quiero. Quiero mostrar lo que tengo configurado para mostrar y luego romper el ciclo, por alguna razón el break; no está funcionando. Código:

import java.util.Scanner; import java.util.Random; public class GuessingGame1_3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); Random rand = new Random(); System.out.print("Pick a number to guess between: "); int userNumber = input.nextInt(); int random = rand.nextInt(userNumber); if (random < 1) { random = random + 1; } else if (random > userNumber) { random = random - 1; } while (true) { System.out.print(''/f''); System.out.print("Pick the amount of attempts you would like to have (Max of 10 attempts): "); int userAttempts = input.nextInt(); if (userAttempts > 10) { System.out.println("To Many Attempts"); System.out.println(""); } else if (userAttempts <= 10) { System.out.println("Version: 1.3"); System.out.println("----------------------------------------------------------------------"); System.out.println("You have " + userAttempts + " attempts to guess the number or else you fail. Goodluck!"); System.out.println(""); System.out.println("Type ''begin'' to Begin!"); String start = input.next(); System.out.print(''/f''); if (start.equals("begin")) { for(int i=1; i<userAttempts + 1; i++) { System.out.print("Enter a number between 1-" + userNumber + ": "); int number = input.nextInt(); if (number > random) { System.out.println("Too Big"); System.out.println(""); } else if (number < random) { System.out.println("Too Small"); System.out.println(""); } else if (number == random) { System.out.println("Correct!"); break; } if (i == userAttempts) { System.out.println("You have failed"); System.out.println("Number Was: " + random); break; } } } else if (!start.equals("begin")) { System.out.print(''/f''); System.out.println("Incorrect Command"); System.out.println("Please Exit Console And Retry"); break; } } } } }


Introduzca una variable para verificar si debe continuar el ciclo. Algo como:

public static void main(String[] args) { Scanner input = new Scanner(System.in); Random rand = new Random(); System.out.print("Pick a number to guess between: "); int userNumber = input.nextInt(); int random = rand.nextInt(userNumber); if (random < 1) { random = random + 1; } else if (random > userNumber) { random = random - 1; } boolean continueGame = true; while (continueGame) { System.out.print(''/f''); System.out .print("Pick the amount of attempts you would like to have (Max of 10 attempts): "); int userAttempts = input.nextInt(); if (userAttempts > 10) { System.out.println("To Many Attempts"); System.out.println(""); } else if (userAttempts <= 10) { System.out.println("Version: 1.3"); System.out .println("----------------------------------------------------------------------"); System.out .println("You have " + userAttempts + " attempts to guess the number or else you fail. Goodluck!"); System.out.println(""); System.out.println("Type ''begin'' to Begin!"); String start = input.next(); System.out.print(''/f''); if (start.equals("begin")) { for (int i = 1; i < userAttempts + 1; i++) { System.out.print("Enter a number between 1-" + userNumber + ": "); int number = input.nextInt(); if (number > random) { System.out.println("Too Big"); System.out.println(""); } else if (number < random) { System.out.println("Too Small"); System.out.println(""); } else if (number == random) { System.out.println("Correct!"); continueGame = false; } if (i == userAttempts) { System.out.println("You have failed"); System.out.println("Number Was: " + random); continueGame = false; } } } else { //You may remove if (!start.equals("begin")) check //as the result is either true or false and you //have checked the true condition in if System.out.print(''/f''); System.out.println("Incorrect Command"); System.out.println("Please Exit Console And Retry"); continueGame = false; } } } }


Necesita romper el bucle externo y los bucles internos

Eche un vistazo: (variable failattempt )

import java.util.Scanner; import java.util.Random; public class GuessingGame1_3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); Random rand = new Random(); System.out.print("Pick a number to guess between: "); int userNumber = input.nextInt(); int random = rand.nextInt(userNumber); if (random < 1) { random = random + 1; } else if (random > userNumber) { random = random - 1; } boolean failattempt = false; while (true) { System.out.print(''/f''); System.out.print("Pick the amount of attempts you would like to have (Max of 10 attempts): "); int userAttempts = input.nextInt(); if (userAttempts > 10) { System.out.println("To Many Attempts"); System.out.println(""); } else if (userAttempts <= 10) { System.out.println("Version: 1.3"); System.out.println("----------------------------------------------------------------------"); System.out.println("You have " + userAttempts + " attempts to guess the number or else you fail. Goodluck!"); System.out.println(""); System.out.println("Type ''begin'' to Begin!"); String start = input.next(); System.out.print(''/f''); if (start.equals("begin")) { for(int i=1; i<userAttempts + 1; i++) { System.out.print("Enter a number between 1-" + userNumber + ": "); int number = input.nextInt(); if (number > random) { System.out.println("Too Big"); System.out.println(""); } else if (number < random) { System.out.println("Too Small"); System.out.println(""); } else if (number == random) { System.out.println("Correct!"); break; } if (i == userAttempts) { System.out.println("You have failed"); System.out.println("Number Was: " + random); failattempt = true; break; } } if (failattempt == true) break; } else if (!start.equals("begin")) { System.out.print(''/f''); System.out.println("Incorrect Command"); System.out.println("Please Exit Console And Retry"); break; } } } } }