How can I pass this exercise to for or while loops

0

How could I transform this exercise only to for or while loops? Thank you. I leave the code below.

Sentence:

Create a program that "draws" a square formed by figures successive, with the size indicated by the user, up to maximum of 9. For example, if you want size 5, the square would be like this:

11111
22222
33333
44444
55555

Code:

package entrega3;
import java.util.Scanner;
public class Entrega3 {
 public static void main(String[] args) {
 //Inicialización de variables
 int num = 0;
 String cuadrado = "";
 Scanner scanNum = new Scanner(System.in);

 //Solución
 System.out.print("Introduce un número entre 0 y 9: ");
 num = scanNum.nextInt();
 while(num>9 || num<0){
 if(num>9){
 System.out.print("Introduce un número menor que 9, por favor: ");
 num = scanNum.nextInt();
 }else{
 System.out.print("Introduce un número mayor que 0, por favor: ");
 num = scanNum.nextInt();
 }
 }
 switch(num){
 case 1: System.out.println("1");
 break;
 case 2: System.out.println("11");
 System.out.println("22");
 break;
 case 3: System.out.println("111");
 System.out.println("222");
 System.out.println("333");
 break;
 case 4: System.out.println("1111");
 System.out.println("2222");
 System.out.println("3333");
 System.out.println("4444");
 break;
 case 5: System.out.println("11111");
 System.out.println("22222");
 System.out.println("33333");
 System.out.println("44444");
 System.out.println("55555");
 break;
 case 6: System.out.println("111111");
 System.out.println("222222");
 System.out.println("333333");
 System.out.println("444444");
 System.out.println("555555");
 System.out.println("666666");
 break;
 case 7: System.out.println("1111111");
 System.out.println("2222222");
 System.out.println("3333333");
 System.out.println("4444444");
 System.out.println("5555555");
 System.out.println("6666666");
 System.out.println("7777777");
 break;
 case 8: System.out.println("11111111");
 System.out.println("22222222");
 System.out.println("33333333");
 System.out.println("44444444");
 System.out.println("55555555");
 System.out.println("66666666");
 System.out.println("77777777");
 System.out.println("88888888");
 break;
 case 9: System.out.println("111111111");
 System.out.println("222222222");
 System.out.println("333333333");
 System.out.println("444444444");
 System.out.println("555555555");
 System.out.println("666666666");
 System.out.println("777777777");
 System.out.println("888888888");
 System.out.println("999999999");
 break;
 default:System.out.println("Introduce un valor numérico");
 break;
 }
 }
}
    
asked by jose manuel 05.11.2017 в 21:09
source

2 answers

2

What you should do is based on the number entered iterate the number of times and get the value, in my case use i = 1 and in each iteration will increase its value. Since we already have the main value now you must with the help of another loop for concatenate the value i n - 1 times. Once the for is finished, print the value and return the chain to normal for the next number.

String cadena = "";
String valor = "";
num = scanNum.nextInt();
for (int i  = 1; i <= num; i++) {
    valor = Integer.toString(i);
    for (int j = 0; j < num; j++){
        cadena += valor;    
    }
    System.out.println(cadena);  
    cadena = "";
}

I advise you to review cycle and practice documentation

    
answered by 05.11.2017 / 23:44
source
0
num = scanNum.nextInt();
for(int z=0;z<num;z++){
  for(int i=1; i<num;i++){
      if(i==num){
         System.out.println(Integer.toString(i));
      }else{
         System.out.print(Integer.toString(i));
      }
  }
}

What this piece of code does is make two fors. The first will be the one that will cause the number of lines requested by the user to be printed. The second will be responsible for printing these lines with a number that will grow according to the index of the for.
The two fors will go from 1 (first number to be taken through the screen) to the number that the user has written.
Being one for inside the other one will get it in square form.
Within the for that makes that the numbers are printed when the index is the same as the number said by the user will make System.out.println () so that it jumps out of line. On the other hand, if this is not equal to the index, it will not jump to the next line so that it remains square.

    
answered by 05.11.2017 в 21:15