Domanda

This is the main step in this assignment. Create nested loops to print out the numbers in a table format if the input is valid. The first input i defines the number
of rows, and the second input j defines the number of columns. Inside the loops, write the code to list up the positive numbers from 1 to i*j. Use the System.out.printf() function with "%4d" and "%4s" formats for the integers and Strings.

I need to print out

Set the size of table and data-type (Type 'Q' or 'q' to quit): 3 4 "numbers"

  • | 1 2 3 4

-------------------

1 | 1 2 3 4

2 | 5 6 7 8

3 | 9 10 11 12

after i set the number of columns and rows but I can only get the inner table, not the outer numbers tables or stars. In order to get it to display correctly I had to alter it but it should be lined up and neat

    Scanner scan = new Scanner(System.in);
    String stringIn;

    do
    {
        System.out.print("Set the size of table and data-type(Type Q or q to quit)");
        stringIn = scan.nextLine();
        int space = stringIn.indexOf(" ");
        int spaceTwo = stringIn.indexOf(" ", space+1);

        if(stringIn.length()>4)
        {

            String firstNum = stringIn.substring(0,space);
            String secondNum = stringIn.substring(space+1,spaceTwo);
            String dataType = stringIn.substring(spaceTwo+1);
            int firstInt = Integer.parseInt(firstNum);
            int secondInt = Integer.parseInt(secondNum);

            if (!stringIn.equals("Q")&&!stringIn.equals("q")&&firstInt>=0&&secondInt>=0)
            {
                System.out.println(stringIn.substring(0,space) + " " + stringIn.substring(space+1,spaceTwo) + " " + stringIn.substring(spaceTwo+1));
            }
            else if(firstInt<0||secondInt<0)
            {
                System.out.println("Try again. The input was invalid");
            }
            for(int i = 1; i <firstInt+1; i++)
            {
                for (int j = 1; j < secondInt+1; j++)
                {
                    System.out.printf("%4d", i*j);
                    System.out.printf("%4s", i*j);
                }
                System.out.println();
            }
        }

    }
    while(!stringIn.equals("Q")&&!stringIn.equals("q"));

This is my first Java class so my code is very messy.

È stato utile?

Soluzione

You're pretty close, just a little off with the logic in your nested loop. This is what I modified:

1) Moved the column label outside the inner loop

2) Created a counter to use for the cell values

3) Use the counter to print the cell value & then incremented it

Code:

String headerRow= " * |"; 
String spacer = "-----";
for(int i=1; i<secondInt + 1; i++){headerRow+="   "+i; spacer+="----";}
System.out.println(headerRow);
System.out.println(spacer);
int counter = 1;
for (int i = 1; i < firstInt + 1; i++) {
    System.out.printf("%4s", i + " |");
    for (int j = 1; j < secondInt + 1; j++) {
        System.out.printf("%4d", counter);
        counter++;
    }
    System.out.println();
}

That code outputs this:

> 4 5 numbers  
* |   1   2   3   4   5
> -------------------------  
1 |   1   2   3   4   5  
2 |   6   7   8   9  10  
3 |  11  12  13  14  15  
4 |  16  17  18  19  20
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top