简体   繁体   English

如何将程序转换为循环程序

[英]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. 现在,我需要做的是生成一个表,其中包含40摄氏度从摄氏温度到华氏温度的转换。 So if the user input 0 as the starting degrees celsius then the output would create a table 因此,如果用户输入0作为起始摄氏度,则输出将创建一个表

Celsius Fahrenheit 摄氏华氏度

0 32.0 0 32.0

1 33.8 1 33.8

2 35.6 2 35.6

3 37.4 3 37.4

4 39.2 4 39.2

5 41.0 5 41.0

etc up to 40. How would I get started and tweak my program to something like this? 等等,直到40。我该如何开始并将程序调整为类似这样的方式?

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 摄氏华氏0.0 32.0

Celsius Fahrenheit 1.0 33.8 摄氏华氏1.0 33.8

What do I change so my output statement looks like the one below? 我要更改什么,以便输出语句如下所示?

Celsius Fahrenheit 摄氏华氏度

0 32.0 0 32.0

1 33.8 1 33.8

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

Something like this should work (pseudoCode) 这样的事情应该可以工作(pseudoCode)

If the user inputs 5 it will range from 5-45, which should be what your question asked for. 如果用户输入5,则其范围为5-45,这应该是您的问题所要求的。

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) 显然,您将需要为用户输入等添加字符串/浮点数(或int,如果仅应输入自然数)转换等(但由于您使用的是扫描仪,因此应该毫无疑问)

EDIT: 编辑:

As to your request in comments, here is the full code: 关于您在注释中的请求,以下是完整的代码:

CtoF.java: 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: 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”时的样本输出:

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

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, 您可以先创建一个2D数组,

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. 注意:您需要一个循环,该循环可自动将温度增加40倍并存储摄氏度,华氏度值。

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]);
    }
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM