archer
archer

Reputation: 89

How do I convert my program into a looping program

Here is what my program looks like so far.

package fahrenheit;

import java.util.Scanner;

/**
*
* @author Steve
*/
public class Fahrenheit {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    // TODO code application logic here
    String name; // city name
    double temperatureInF; // original temperature
    double temperature; // converted temperature

    // declare an instance of Scanner to read the datastream from the keyboard.
Scanner kb = new Scanner(System.in);

    // get name of city
    System.out.println ("Hello, please enter name of city: ");
    name = kb.nextLine();

    // get temperature in Celsius
    System.out.println("Enter current temperature in " + name + " (celsius): ");
    temperature = kb.nextDouble();

    // convert to degrees Fahrenheit
    temperatureInF = ((temperature *9/5)+32);

    // output statement
    System.out.println(" The current temperature in " + name + " is " + temperature + " \u00b0C" + " which is " + temperatureInF + " \u00b0F");
}

}

Now what I need it do is to produce a table of 40 temperature conversions from Celsius to Fahrenheit. So if the user input 0 as the starting degrees celsius then the output would create a table

Celsius Fahrenheit

0 32.0

1 33.8

2 35.6

3 37.4

4 39.2

5 41.0

etc up to 40. How would I get started and tweak my program to something like this?

Edit: Got everything except this very last thing

Here is my output statement

 // output statement
     System.out.println("\nCelsius" + "\t Fahrenheit" + temperatures[i][0] + " " + " " + temperatures[i][1]);
}
}

However this gives me

Celsius Fahrenheit 0.0 32.0

Celsius Fahrenheit 1.0 33.8

What do I change so my output statement looks like the one below?

Celsius Fahrenheit

0 32.0

1 33.8

Upvotes: 0

Views: 766

Answers (2)

showp1984
showp1984

Reputation: 378

for (int i=userInput; i<=40+userInput; i++)
    convertAndOutput(i)

Something like this should work (pseudoCode)

If the user inputs 5 it will range from 5-45, which should be what your question asked for.

Obviously you will need to add a string/float(or int, if there should only natural numbers being entered) conversion for the user input, etc etc... (But since your are using the Scanner that should be a nobrainer)

EDIT:

As to your request in comments, here is the full code:

CtoF.java:

package com.company;

import java.text.DecimalFormat;

public class CtoFLoop {
    public CtoFLoop(int userInput, int until) {
        for (int i = userInput; i <= until + userInput; i++)
            convertCtoFAndOutput(i);
    }

    public static String CtoF(int celsius) {
//      (°C * 1.8000) + 32 = °F
        return new DecimalFormat("#0.00").format(((double) celsius * 1.8000d) + 32d);
    }

    private void convertCtoFAndOutput(int celsius) {
        System.out.println(celsius + "°C" + " = " + CtoF(celsius) + "°F");
    }
}

Main.java:

package com.company;

import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        new CtoFLoop(new Scanner(new InputStreamReader(System.in)).nextInt(), 40);
    }
}

Sample Output, when entering "0":

0°C = 32.00°F
1°C = 33.80°F
2°C = 35.60°F
3°C = 37.40°F
4°C = 39.20°F
5°C = 41.00°F
6°C = 42.80°F
7°C = 44.60°F
8°C = 46.40°F
9°C = 48.20°F
10°C = 50.00°F
11°C = 51.80°F
12°C = 53.60°F
13°C = 55.40°F
14°C = 57.20°F
15°C = 59.00°F
16°C = 60.80°F
17°C = 62.60°F
18°C = 64.40°F
19°C = 66.20°F
20°C = 68.00°F
21°C = 69.80°F
22°C = 71.60°F
23°C = 73.40°F
24°C = 75.20°F
25°C = 77.00°F
26°C = 78.80°F
27°C = 80.60°F
28°C = 82.40°F
29°C = 84.20°F
30°C = 86.00°F
31°C = 87.80°F
32°C = 89.60°F
33°C = 91.40°F
34°C = 93.20°F
35°C = 95.00°F
36°C = 96.80°F
37°C = 98.60°F
38°C = 100.40°F
39°C = 102.20°F
40°C = 104.00°F

Upvotes: 1

Xoul
Xoul

Reputation: 369

I assume you know how to use loops but you are confused about how to add it in your program.

You can first create a 2D array,

double[][] temperatures = new double[41][2];

Then you can place a loop:

for(int i = 0; i < temperatures.length; i++)
{

        temperatures[i][0] = temperature+i; // increments temperature and adds value to array, temperature in celsius
        temperatures[i][1] = (((temperature+i) *9/5)+32); // temperature in Fahrenheit

}

Note: You asked for a loop that automatically increments temperature 40 times and stores Celsius, Fahrenheit values.

package fahrenheit;

import java.util.Scanner;

/**
*
* @author Steve
*/
public class Fahrenheit {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    // TODO code application logic here
    String name; // city name
    double temperature; // temperature in Celsius
    double[][] temperatures = new double[41][2]; // stores temperature values

    // declare an instance of Scanner to read the datastream from the keyboard.
Scanner kb = new Scanner(System.in);

    // get name of city
    System.out.println ("Hello, please enter name of city: ");
    name = kb.nextLine();

    // get temperature in Celsius
    System.out.println("Enter current temperature in " + name + " (celsius): ");
    temperature = kb.nextDouble();

    for(int i = 0; i < temperatures.length; i++)
    {

        temperatures[i][0] = temperature+i; // increments temperature and adds value to array, temperature in celsius
        temperatures[i][1] = (((temperature+i) *9/5)+32); // temperature in Fahrenheit

    }

    System.out.println("\nCelsius" + "\t Fahrenheit");
    for(int i = 0; i < temperatures.length; i++)
    {

        // output statement
        System.out.println(temperatures[i][0] + " " + " " + temperatures[i][1]);
    }
}

}

Upvotes: 1

Related Questions