最简单的方式用C表示摄氏温度和华氏温度的转换

最简单的方式用C表示摄氏温度和华氏温度的转换,做一些简单的修改:

#include<stdio.h>
#define lower 0
#define upper 300
#define step 20


main()
{
int fahr;


for (fahr = lower; fahr <= upper; fahr = fahr + step)
printf("%d\t%5.1f\n", fahr, (5.0 / 9.0)*(fahr - 32));
}


计算结果如下:

0       -17.8
20       -6.7
40        4.4
60       15.6
80       26.7
100      37.8
120      48.9
140      60.0
160      71.1
180      82.2
200      93.3
220     104.4
240     115.6
260     126.7
280     137.8
300     148.9
请按任意键继续. . .

该程序允许用户比较摄氏和华氏温度的相等性。具体要如下: 建立一个Temperature类,它有两个实例变量:一个是温度值(float);另一个是温度的计量单位(char),即取值 ’C’ 或 ’F’,C表示Celsius(摄氏温度)、F表示Fahrenheit(华氏温度)。这个类还应有2 个构造方法: 给每一个实例变量设置缺省值,即温度值为0、计量单位为C(摄氏); 通过两个参数分别给两个实例变量赋初值。 这个类应包括: 两个存取方法:一个返回用摄氏计量的温度、另一个返回华氏温度。摄氏和华氏温度转换公式如下: degreesC = 5 * (degreesF - 32)/9 degreesF = (9* (degreesC)/5) + 32 三个改变对象属性的方法:一个设置温度值、一个设置计量单位(’F’或’C’)、另一个同时设置温度值和计量单位。 三个比较方法:一个用来是否两个温度相等;一个方法用来测试比较一个温度大于另一个温度;一个方法用来测试比较一个温度小于另一个温度。 最后写一个测试程序(main方法),测试Temperature类中的所有方法。要确保使用到所用的构造方法;要测试每一个比较方法,并确保至少有一个真(true)一个假(false)的结果。例如测试:0.0 degrees C = 32.0 degrees F -40.0 degrees C = - 40.0 degrees F -100.0 degrees C = 212.0 degrees F 请注意:必须保证输入的数据是有效的。所有非法的输入字符都应产生相应的错误信息并提示用户重新输入正确的数据。
以下是一个简单的Java Swing界面,用于将华氏温度转换摄氏温度,或将摄氏温度转换华氏温度。 ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TemperatureConverter extends JFrame implements ActionListener { private JTextField inputField, outputField; private JRadioButton fahrenheitButton, celsiusButton; public TemperatureConverter() { // 创建界面 setTitle("温度转换器"); setSize(300, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(3, 2)); // 创建输入框和标签 JLabel inputLabel = new JLabel("输入温度:"); inputField = new JTextField(""); add(inputLabel); add(inputField); // 创建输出框和标签 JLabel outputLabel = new JLabel("转换结果:"); outputField = new JTextField(""); outputField.setEditable(false); add(outputLabel); add(outputField); // 创建单选按钮组 ButtonGroup group = new ButtonGroup(); fahrenheitButton = new JRadioButton("华氏度"); celsiusButton = new JRadioButton("摄氏度"); fahrenheitButton.setSelected(true); group.add(fahrenheitButton); group.add(celsiusButton); add(fahrenheitButton); add(celsiusButton); // 创建转换按钮 JButton convertButton = new JButton("转换"); convertButton.addActionListener(this); add(convertButton); // 显示界面 setVisible(true); } public void actionPerformed(ActionEvent e) { // 获取输入温度值 String inputText = inputField.getText(); if (inputText.isEmpty()) { JOptionPane.showMessageDialog(this, "请输入温度值"); return; } // 将温度转换为数值 double temperature; try { temperature = Double.parseDouble(inputText); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(this, "请输入有效的温度值"); return; } // 判断选中的温度单位 if (fahrenheitButton.isSelected()) { // 转换华氏度为摄氏度 double result = (temperature - 32) * 5 / 9; outputField.setText(String.format("%.2f 摄氏度", result)); } else { // 转换摄氏度为华氏度 double result = temperature * 9 / 5 + 32; outputField.setText(String.format("%.2f 华氏度", result)); } } public static void main(String[] args) { new TemperatureConverter(); } } ``` 该界面包含一个输入框、一个输出框、两个单选按钮和一个转换按钮。用户输入温度值后,选择华氏度或摄氏度,点击转换按钮后,程序将温度转换为另一种单位,并将转换结果显示在输出框中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值