How to print off answers from For loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Charles Sherman
    New Member
    • Dec 2010
    • 4

    How to print off answers from For loops

    I am writing a program that I have to take the length of a rectangle (1-10) and multiply them by the width(1-10) I am using a for loop and this is what I have so far
    Code:
      public class area
      {
      public static void main(Strings args[])
      {
      int height;
      int width;
      int area;
      int i;
      int e;
      for (i=1; i>=11; i++);
      height = i;
      for (e=1; e>=11; e++);
      width = e;
      (area) = (height) * (width);
      System.out.print (area);}
    }
    the error is in line 14 and it is saying "variable height might not have been initialized."
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    First of all the two for loops do not make any sense.

    Code:
    for (i=1; i>=11; i++);
    height = i;
    for (e=1; e>=11; e++);
    width = e;
    If you put the semi colon at the end of the for loop, loop will be terminated it won't execute.

    putting a semi colon at the end of for loop make
    Code:
    height = 1
    So in the next loop even width will be set to 1 (Remember you placed a semi colon to the second for loop as well).
    Code:
    width=1;
    So area is also 1.

    Also change

    Code:
    public static void main(Strings args[])
    to
    Code:
    public static void main(String args[])
    Regards
    Dheeraj Joshi

    Comment

    Working...