Problem with a while loop

0

Good, given the following code, it resumes that although you write as high -1 and as width 30, you still execute code and I would leave, in this example, the last thing.

I want to know how to prohibit the minor that 1 and the greater than 20 so that the code is not executed, his would be doing it with for, but it is worth to me with a while and then I transform it into for.

import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Cristobal
 */


public class Ejercicio6_2Try {
public static int alto;
public static int ancho;
public static String guiones = "-";
public static String asteriscos ="*";
public static String rectangulo, altorectangulo, espaciostotal;
public static String espacios =" ";

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

//for (;((alto>1) || (alto<20) ) && ((ancho>1) || (ancho<20));){

while ((alto <= 1) || (ancho <= 1) || (alto > 20) || (ancho > 20)){

try

    {


            System.out.println("Escribe el alto del marco, entre 1 y 20");
            alto = sc.nextInt();
            int i = alto;

            System.out.println("Escribe el ancho del marco, entre 1 y 20");
            ancho = sc.nextInt();

            switch (alto){
            case 1:

            switch (ancho) {
                case 1:
                    rectangulo = new String(new char[ancho]).replace("
run:
Escribe el alto del marco, entre 1 y 20
-1
Escribe el ancho del marco, entre 1 y 20
30
+------------------------------+
|******************************|
|******************************|
+------------------------------+
BUILD SUCCESSFUL (total time: 1 second)
", guiones); System.out.println("+"); break; case 2: rectangulo = new String(new char[ancho]).replace("
import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Cristobal
 */


public class Ejercicio6_2Try {
public static int alto;
public static int ancho;
public static String guiones = "-";
public static String asteriscos ="*";
public static String rectangulo, altorectangulo, espaciostotal;
public static String espacios =" ";

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

//for (;((alto>1) || (alto<20) ) && ((ancho>1) || (ancho<20));){

while ((alto <= 1) || (ancho <= 1) || (alto > 20) || (ancho > 20)){

try

    {


            System.out.println("Escribe el alto del marco, entre 1 y 20");
            alto = sc.nextInt();
            int i = alto;

            System.out.println("Escribe el ancho del marco, entre 1 y 20");
            ancho = sc.nextInt();

            switch (alto){
            case 1:

            switch (ancho) {
                case 1:
                    rectangulo = new String(new char[ancho]).replace("
run:
Escribe el alto del marco, entre 1 y 20
-1
Escribe el ancho del marco, entre 1 y 20
30
+------------------------------+
|******************************|
|******************************|
+------------------------------+
BUILD SUCCESSFUL (total time: 1 second)
", guiones); System.out.println("+"); break; case 2: rectangulo = new String(new char[ancho]).replace("%pre%", guiones); System.out.println("++"); break; default: rectangulo = new String(new char[ancho]).replace("%pre%", guiones); System.out.println("+" + rectangulo + "+"); break; } break; case 2: break; case 3: break; case 4: break; case 5: break; default: rectangulo = new String(new char[ancho]).replace("%pre%", guiones); System.out.println("+" + rectangulo + "+"); altorectangulo = new String(new char[ancho]).replace("%pre%", asteriscos); System.out.println("|" + altorectangulo + "|"); for (;((i>4) && (i<=20) && (ancho<=20));){ i--; espaciostotal = new String(new char[ancho]).replace("%pre%", espacios); System.out.println("|" + espaciostotal + "|"); } altorectangulo = new String(new char[ancho]).replace("%pre%", asteriscos); System.out.println("|" + altorectangulo + "|"); rectangulo = new String(new char[ancho]).replace("%pre%", guiones); System.out.println("+" + rectangulo + "+"); } } catch (Exception e) { System.out.println("Has introducido un valor incorrecto"); sc.nextLine(); } } } }
", guiones); System.out.println("++"); break; default: rectangulo = new String(new char[ancho]).replace("%pre%", guiones); System.out.println("+" + rectangulo + "+"); break; } break; case 2: break; case 3: break; case 4: break; case 5: break; default: rectangulo = new String(new char[ancho]).replace("%pre%", guiones); System.out.println("+" + rectangulo + "+"); altorectangulo = new String(new char[ancho]).replace("%pre%", asteriscos); System.out.println("|" + altorectangulo + "|"); for (;((i>4) && (i<=20) && (ancho<=20));){ i--; espaciostotal = new String(new char[ancho]).replace("%pre%", espacios); System.out.println("|" + espaciostotal + "|"); } altorectangulo = new String(new char[ancho]).replace("%pre%", asteriscos); System.out.println("|" + altorectangulo + "|"); rectangulo = new String(new char[ancho]).replace("%pre%", guiones); System.out.println("+" + rectangulo + "+"); } } catch (Exception e) { System.out.println("Has introducido un valor incorrecto"); sc.nextLine(); } } } }

This is the code output with -1 30, and nothing should be output because if it runs only when it is larger:

%pre%     
asked by Cristobal Ruiz 03.11.2017 в 19:33
source

3 answers

0

Try to initialize the variables

        System.out.println("Escribe el alto del marco, entre 1 y 20");
        alto = sc.nextInt();
        int i = alto;

        System.out.println("Escribe el ancho del marco, entre 1 y 20");
        ancho = sc.nextInt();

outside the while loop (). Declare them before entering the while

    
answered by 03.11.2017 в 20:52
0

You can do a do while before switch statements

do {
    System.out.println("Escribe el alto del marco, entre 1 y 20");
    alto = sc.nextInt();
    int i = alto;

    System.out.println("Escribe el ancho del marco, entre 1 y 20");
    ancho = sc.nextInt();
 }while ((alto <= 1) || (ancho <= 1) || (alto > 20) || (ancho > 20))
    
answered by 04.11.2017 в 21:31
0

I think that your White loop has bad conditions since you are allowing to enter, in addition to before declaring the values for the variables (you must declare the input before checking the White.

// first declares and collects the entries by keyboard here

// now put the condition to enter the loop, which is when the values are between 1 and 20. Putting! In front of the variable is like denying (while high is not less than 1 ... and so on. while ((! high 20)) { // Another option is to change all the signs because they are declared the other way around. // while ((high> = 1) || (width > = 1) || (high

answered by 08.11.2017 в 16:08