Java 中怎么用对话框输入三个数 并用对话框输出其平均值 帮忙写一下程序

2024-11-28 17:37:43
推荐回答(3个)
回答1:

import java.util.Scanner;

import javax.swing.JOptionPane;

public class TT
{

/**
* @param args
*/
public static void main(String[] args)
{
// 对话框
double d_1, d_2, d_3;
do
{
String d1_str = JOptionPane.showInputDialog("input first:");
try
{
d_1 = Double.valueOf(d1_str);
break;
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "error reinput first");
}
} while (true);

do
{
String d2_str = JOptionPane.showInputDialog("input second:");
try
{
d_2 = Double.valueOf(d2_str);
break;
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "error reinput second");
}
} while (true);

do
{
String d3_str = JOptionPane.showInputDialog("input third:");
try
{
d_3 = Double.valueOf(d3_str);
break;
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "error reinput third");
}
} while (true);

double d_r = (d_1 + d_2 + d_3) / 3;
JOptionPane.showMessageDialog(null, "the result:" + d_r);

// 控制台
Scanner s = new Scanner(System.in);
System.out.println("input first:");
double d1 = s.nextDouble();
System.out.println("input second:");
double d2 = s.nextDouble();
System.out.println("input third:");
double d3 = s.nextDouble();

double d = (d1 + d2 + d3) / 3;
System.out.println("the result: " + d);
}
}

回答2:

Scanner sc = new Scanner(System.in);
System.out.print("请输入第一个数:");
double num1=sc.nextDouble();
System.out.print("请输入第二个数:");
double num2=sc.nextDouble();
System.out.print("请输入第三个数:");
double num3=sc.nextDouble();
System.out.print("平均数:"+(num1+num2+num3)/3+"\n");

回答3:

楼上大哥已经写好了。。你看看怎么样。可以的话采纳一下。。。