Home > Software engineering >  JAVA - Using the for loop to display inches to meters conversion table. Why is the test expression i
JAVA - Using the for loop to display inches to meters conversion table. Why is the test expression i

Time:09-17

So I recently started learning JAVA and I'm very new to coding. This is one of the questions in the exercises:

  • Adapt Try This 1­-2 so that it prints a conversion table of inches to meters. Display 12 feet of conversions, inch by inch. Output a blank line every 12 inches. (One meter equals approximately 39.37 inches.)

Solution

class InchesToMeters{
    public static void main(String[] args){
    double meters; double inches;  
    int counter;

    
    counter = 0;
    for(inches = 1; inches <= 144; inches  ){
    meters = 1/39.37;
    System.out.println(inches   " inches "   " is "   meters    " meters");
    counter  ;
    }
    if(counter == 12){
    System.out.println();   
    counter = 0;
    }
    }
}

My solution is similar to the code given above. But I'm not able to understand, why the test expression in the for loop has inches <= 144;. I'm curious why they have got inches <= 144; .

CodePudding user response:

Problem:

 Display 12 feet of conversions, inch by inch. 

How many inches in 12 feet? 12 x 12 = 144.

CodePudding user response:

class InchesToMeters{
public static void main(String[] args){
double meters; double inches;  
int counter;


counter = 1;
for(inches = 1; inches <= 144; inches  ){
meters = inches/39.37;
System.out.println(inches   " inches "   " is "   meters    " meters");
counter  ;
    if(counter == 12){
    System.out.println();   
    counter = 1;
    }
}

}

}

  • Related