Java简单题 如何使结果保留小数点一位

来源:百度知道 编辑:UC知道 时间:2024/06/22 01:02:33
import javax.swing.JOptionPane;
import java.math.*;

public class wendu {
public static void main(String[] args) {
double huashi;
double sheshi;

// Prompt the user to enter number
String str = JOptionPane.showInputDialog(
null, "请输入一个华氏温度", "华氏温度/摄氏温度转换",
JOptionPane.QUESTION_MESSAGE);

huashi = Double.parseDouble(str);
sheshi = (5.0/9)*(huashi-32);

JOptionPane.showMessageDialog(
null,"华氏温度" + huashi + '\n' + "相应的摄氏温度" + sheshi,"华氏温度/摄氏温度转换",
JOptionPane.INFORMATION_MESSAGE);
}
}

import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class wendu {
public static void main(String[] args) {
double huashi;
double sheshi;

// Prompt the user to enter number
String str = JOptionPane.showInputDialog(null, "请输入一个华氏温度",
"华氏温度/摄氏温度转换", JOptionPane.QUESTION_MESSAGE);

huashi = Double.parseDouble(str);
sheshi = (5.0 / 9) * (huashi - 32);
DecimalFormat format = new DecimalFormat("#0.0"); //java.text包里面的DecimalFormat 类
String buf = format.format(sheshi).toString();

JOptionPane.showMessageDialog(null, "华氏温度" + huashi + '\n' + "相应的摄氏温度"
+ buf, "华氏温度/摄氏温度转换", JOptionPane.INFORMATION_MESSAGE);
}
}

import javax.swing.JOptionPane;
import java.math.*;

public class Print {
public static void main(String[] args) {
double huashi;
double sheshi;