For loop to make a table from 1 to 100 (code in desc)

edited October 12 in Questions about Code

Hi !

Very new to programming and I'm struggling to understand what I'm doing wrong. What I want to do is demonstrate how to use for loops to create a table of the numbers 1 to 100.

Here is what the program should look like: Numbers_1-100

Here is my progress so far: Progress

size(400,400);
float x = 30;
float y = 40;

fill(0);
for(int k = 0; k < 10; k++)
{
  //One row from 1-10
  for (int i = 0; i < 10; i++) 
  {
    text("1", x + i * 20, y + k * 20);
  }
}

Answers

  • ... so, you have a program that is printing "1" over and over...

    What variable expression should replace that "1" with? What specific variables -- variable that is already in your code -- could be used to count up to 100...?

    For example, you want the number on the 3rd row and the 8th column to be "28". What is k equal to when you are trying to print "28"? What about i?

  • To add to @jeremydouglass's comment, you can add a println() statement inside your for loop. See below.

    After you figure out the code, you might consider adding the tab character to keep the columns of your table aligned: https://docs.oracle.com/javase/tutorial/java/data/characters.html

    Example: println("Check these numbers: \n \t "+nfs(3,0,0)+"\n\t "+nfs(38,0,0)+"\n\t "+nfs(00,0,0)+"\n\t "+ nfs(1001,0,0));

    println("Check these numbers: \n \t3\n\t38\n\t00\n\t1001");

    You can apply this to the text() function as well.

    Kf

    for(int k = 0; k < 10; k++)
    {
      //One row from 1-10
      for (int i = 0; i < 10; i++) 
      {
        text("1", x + i * 20, y + k * 20);
    
         if(i==8 && k==3){
             println("i="+i);
             println("k="+k);
             println("What is i*k="+"What should go here?");
         }
      }
    }
    
Sign In or Register to comment.