Problem: Write a program that creates a conversion table from kilograms to pounds. The program should prompt the user for the starting point of the table in kilograms and list ten entries in increments of one kilogram. The conversion factor is 1 kilogram is equal to 2.2 pounds.
I need to use a for-loop for this program. I tested with inputs 20, 21, 22.
My output is all messed up, the 20 is over the kilograms column when it should be below. The last input, 22, is ending the program when it should let me enter all numbers between 20 and 30 before it ends. The column pounds is not being calculated. Is there something I'm missing?
The output link is
*/import java.util.Scanner;
public class kgToLbs1
{
public static void main(String[] args) {
final double LB_PER_KG = 2.2;
//Algorithm
//Open keyboard for input
Scanner input = new Scanner(System.in);
//Prompt user for beginning value and assign to begVal
System.out.println("Enter beginning value ===> ");
int begVal = input.nextInt();
//Print conversion table headings
System.out.println("Kilograms Pounds");
int kg = input.nextInt();
double lb = input.nextDouble();
//endVal = begVal 9
int endValue = begVal 9;
for (int i = 20; i <= 30; i ) {
System.out.printf("%d%.2f\n", i , (i * LB_PER_KG));
}
//Print conversion table footer
System.out.println("End");
//Close terminal
CodePudding user response:
it should let me enter all numbers between 20 and 30
No, that's not what the instructions say. It says prompt for any starting value, then increment up by one for the next ten values. In other words, print only, not prompt for input
the 20 is over the kilograms column when it should be below
There is a 20 below. There just isn't a space after it because the printf didn't include a space
The column pounds is not being calculated
It is. 2044.00
is printing 20
(kg) and 44.00
(lbs) without a space
I think you're looking for this
final double LB_PER_KG = 2.2;
Scanner input = new Scanner(System.in);
System.out.print("Enter beginning value ===> ");
int begVal = input.nextInt();
System.out.println("Kilograms\tPounds");
for (int i = begVal; i <= begVal 10; i ) {
System.out.printf("%d\t%.2f\n", i , (i * LB_PER_KG));
}