帮忙做一道JAVA的题目
5.MinMaxAverageWriteaprogramthataskstheusertoenteratotalofthreedecimalnumbers.Afterth...
5. Min Max Average
Write a program that asks the user to enter a total of three decimal numbers. After the numbers are entered, the program should ask the user to do the following: Enter 1 to find the maximum of three numbers, enter 2 to find the minimum, or enter 3 to find the average. The result should be displayed along with the three original numbers.
我已经写了这些
String indata= JOptionPane.showInputDialog("enter a total of three decimal numbers,now is the first");
num1 = Double.parseDouble(indata);
String indata1= JOptionPane.showInputDialog("enter the second number");
num2 = Double.parseDouble(indata1);
String indata2= JOptionPane.showInputDialog("enter the third number");
num3 = Double.parseDouble(indata2);
String indata3= JOptionPane.showInputDialog("enter 1 to find out the maximum,enter 2 to findout the minimun,enter 3 to find out the average");
num = Double.parseDouble(indata3);
if(num == 1)
if(num1>num2&num1>num3)
{JOptionPane.showMessageDialog( null," the maximun number is " + num1,"maximun number", }
if(num2>num1&num2>num3)
{JOptionPane.showMessageDialog( null," the maximun number is " + num2,"maximun number", }
if(num3>num2&num3>num1)
{JOptionPane.showMessageDialog( null," the maximun number is " + num3,"maximun number", } 展开
Write a program that asks the user to enter a total of three decimal numbers. After the numbers are entered, the program should ask the user to do the following: Enter 1 to find the maximum of three numbers, enter 2 to find the minimum, or enter 3 to find the average. The result should be displayed along with the three original numbers.
我已经写了这些
String indata= JOptionPane.showInputDialog("enter a total of three decimal numbers,now is the first");
num1 = Double.parseDouble(indata);
String indata1= JOptionPane.showInputDialog("enter the second number");
num2 = Double.parseDouble(indata1);
String indata2= JOptionPane.showInputDialog("enter the third number");
num3 = Double.parseDouble(indata2);
String indata3= JOptionPane.showInputDialog("enter 1 to find out the maximum,enter 2 to findout the minimun,enter 3 to find out the average");
num = Double.parseDouble(indata3);
if(num == 1)
if(num1>num2&num1>num3)
{JOptionPane.showMessageDialog( null," the maximun number is " + num1,"maximun number", }
if(num2>num1&num2>num3)
{JOptionPane.showMessageDialog( null," the maximun number is " + num2,"maximun number", }
if(num3>num2&num3>num1)
{JOptionPane.showMessageDialog( null," the maximun number is " + num3,"maximun number", } 展开
3个回答
展开全部
//做好个GUI图形界面的,楼主拿去玩玩吧~~
// NumberTestGUI.java
import javax.swing.*;
public class NumberTestGUI{
int len;
int[] a;
int count;
NumberTestGUI(){
try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exception e){}
JOptionPane.getRootFrame().setAlwaysOnTop(true);
int cont = JOptionPane.showConfirmDialog(null,"这是一个对您输入的几个整形数进行分析,然后计算最大值,最小值和平均值的小程序,是否继续?","提示消息:",JOptionPane.OK_OPTION);
if(cont!=JOptionPane.OK_OPTION)return;
int tmp = 0;
while(tmp<3){
String rv = JOptionPane.showInputDialog(null," 请先预先设置您要输入的整形数的个数,至少3个: ","3");
try{
tmp = Integer.valueOf(rv.trim());
}catch(Exception e){
JOptionPane.showMessageDialog(null,"请输入一个整形数!","输入错误:",JOptionPane.WARNING_MESSAGE);
}
}
len = tmp;
a = new int[len];
while(count<len){
String t = JOptionPane.showInputDialog("现在请输入整数,目前已输入 "+count+" 组,还要输入 "+(len-count)+" 组:" +
"\n"+(count==0?"":"您的输入:"+getItemStr()));
try{
int v = Integer.valueOf(t);
a[count++]=v;
}catch(Exception e){}
}
boolean loop=true;
int func = 0;
while(loop){
String t = JOptionPane.showInputDialog(null,"您输入的整数如下:\n"+getItemStr()+"\n求最大值请输入 1,求最小值请输入 2,求平均值请输入 3:");
try{
int ti = Integer.valueOf(t);
if(ti<1||ti>3)throw new Exception();
func=ti;
}catch(Exception e){
JOptionPane.showMessageDialog(null,"请输入一个1~3的整形数!","输入错误:",JOptionPane.WARNING_MESSAGE);
continue;
}
String msg=null;
switch(func){
case 1:msg=getMax();break;
case 2:msg=getMin();break;
case 3:msg=getAvg();break;
}
int retry = JOptionPane.showConfirmDialog(null,msg,"计算结果:",JOptionPane.OK_CANCEL_OPTION);
loop=retry==JOptionPane.OK_OPTION;
}
}
private String getMax(){
int max = Integer.MIN_VALUE;
for(int i=0; i<len; i++)max=max<a[i]?a[i]:max;
return "您输入的整数是:\n"+getItemStr()+" \n其中最大的是:"+max+", 是否继续其它计算操作?";
}
private String getMin(){
int min = Integer.MAX_VALUE;
for(int i=0; i<len; i++)min=min>a[i]?a[i]:min;
return "您输入的整数是:\n"+getItemStr()+" \n其中最小的是:"+min+", 是否继续其它计算操作?";
}
private String getAvg(){
int avg = 0;
for(int i=0; i<len; i++)avg+=a[i];
return "您输入的整数是:\n"+getItemStr()+" \n平均值是:"+(float)avg/len+", 是否继续其它计算操作?";
}
private String getItemStr(){
String s = "";
for(int i=0; i<count; i++)
s+=a[i]+",";
return s.substring(0,s.length()-1);
}
public static void main(String[] args) {
new NumberTestGUI();
}
}
// NumberTestGUI.java
import javax.swing.*;
public class NumberTestGUI{
int len;
int[] a;
int count;
NumberTestGUI(){
try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exception e){}
JOptionPane.getRootFrame().setAlwaysOnTop(true);
int cont = JOptionPane.showConfirmDialog(null,"这是一个对您输入的几个整形数进行分析,然后计算最大值,最小值和平均值的小程序,是否继续?","提示消息:",JOptionPane.OK_OPTION);
if(cont!=JOptionPane.OK_OPTION)return;
int tmp = 0;
while(tmp<3){
String rv = JOptionPane.showInputDialog(null," 请先预先设置您要输入的整形数的个数,至少3个: ","3");
try{
tmp = Integer.valueOf(rv.trim());
}catch(Exception e){
JOptionPane.showMessageDialog(null,"请输入一个整形数!","输入错误:",JOptionPane.WARNING_MESSAGE);
}
}
len = tmp;
a = new int[len];
while(count<len){
String t = JOptionPane.showInputDialog("现在请输入整数,目前已输入 "+count+" 组,还要输入 "+(len-count)+" 组:" +
"\n"+(count==0?"":"您的输入:"+getItemStr()));
try{
int v = Integer.valueOf(t);
a[count++]=v;
}catch(Exception e){}
}
boolean loop=true;
int func = 0;
while(loop){
String t = JOptionPane.showInputDialog(null,"您输入的整数如下:\n"+getItemStr()+"\n求最大值请输入 1,求最小值请输入 2,求平均值请输入 3:");
try{
int ti = Integer.valueOf(t);
if(ti<1||ti>3)throw new Exception();
func=ti;
}catch(Exception e){
JOptionPane.showMessageDialog(null,"请输入一个1~3的整形数!","输入错误:",JOptionPane.WARNING_MESSAGE);
continue;
}
String msg=null;
switch(func){
case 1:msg=getMax();break;
case 2:msg=getMin();break;
case 3:msg=getAvg();break;
}
int retry = JOptionPane.showConfirmDialog(null,msg,"计算结果:",JOptionPane.OK_CANCEL_OPTION);
loop=retry==JOptionPane.OK_OPTION;
}
}
private String getMax(){
int max = Integer.MIN_VALUE;
for(int i=0; i<len; i++)max=max<a[i]?a[i]:max;
return "您输入的整数是:\n"+getItemStr()+" \n其中最大的是:"+max+", 是否继续其它计算操作?";
}
private String getMin(){
int min = Integer.MAX_VALUE;
for(int i=0; i<len; i++)min=min>a[i]?a[i]:min;
return "您输入的整数是:\n"+getItemStr()+" \n其中最小的是:"+min+", 是否继续其它计算操作?";
}
private String getAvg(){
int avg = 0;
for(int i=0; i<len; i++)avg+=a[i];
return "您输入的整数是:\n"+getItemStr()+" \n平均值是:"+(float)avg/len+", 是否继续其它计算操作?";
}
private String getItemStr(){
String s = "";
for(int i=0; i<count; i++)
s+=a[i]+",";
return s.substring(0,s.length()-1);
}
public static void main(String[] args) {
new NumberTestGUI();
}
}
展开全部
写一个程序,这个程序要求用户输入三个十进制数字,在这些数字输入之后,程序会告诉用户做以下的事情:输入1查找这三个数字的最大值,输入2查找它们的最小值输入3查找它们的平均值,结果必须和三个数字一起显示出来.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
写一个程序,这个程序要求用户输入三个十进制数字,在这些数字输入之后,程序会告诉用户做以下的事情:输入1查找这三个数字的最大值,输入2或者3查找它们的平均值,结果必须和三个数字一起显示出来.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询