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%