Java编程、输入数字个数、平均数、最小值、最大值减去最小值、

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java编程、输入数字个数、平均数、最小值、最大值减去最小值、相关的知识,希望对你有一定的参考价值。

write a JAVA program to read in a sequence of integers and print out the following quantities, each on a new line and in the following order, your program should be:
1) the number of integers read in.
2) the average value- which need not be an integer. 注意,平均值不是实数!!
3) the minimum value of the integers.
4) the maximum difference between any of the integers.

以上要求顺序不能颠倒、跪求!!!!

测试通过,基本满足你题目要求,写得不太好
import java.util.*;
import java.util.regex.*;
public class t10

public static void main(String [] args)throws InterruptedException

Scanner in=new Scanner(System.in);
System.out.println("输入一组数,用逗号分开");
String s = in.next();
number a = new number(s);
average b = new average(s);
min c =new min(s);
dif d= new dif(s);
Thread t1=new Thread(a);
Thread t2=new Thread(b);
Thread t3=new Thread(c);
Thread t4=new Thread(d);
t1.start();
t2.start();
t3.start();
t4.start();


//整数个数
class number implements Runnable

int sum=0;
List<Double> dlist=new ArrayList<Double>();
public number(String a)

String[] str = a.split(",");
for(int i = 0; i < str.length; i++)

Pattern pattern = Pattern.compile("[0-9]+");
Matcher matcher = pattern.matcher((CharSequence) str[i]);
boolean result = matcher.matches();
if (result)
dlist.add(Double.parseDouble(str[i]));

sum=dlist.size();

public void run()

System.out.println("整数的个数为:"+sum);


//平均数
class average implements Runnable

double sum=0;
double aver=0;
public average(String b)


String[] str = b.split(",");
double [] num =new double[str.length];
for(int i = 0; i < str.length; i++)

num[i]=Double.parseDouble(str[i]);

for (int i = 0; i < num.length; i++)

sum=sum+num[i];

aver=(double)(sum/num.length);


public void run()

try
Thread.sleep(100);
catch(InterruptedException e)

e.printStackTrace();

System.out.println("平均数为:"+aver);


//最小的整数
class min implements Runnable

double m=0;
List<Double> dlist=new ArrayList<Double>();
public min(String c)

String[] str = c.split(",");
for(int i = 0; i < str.length; i++)

Pattern pattern = Pattern.compile("[0-9]+");
Matcher matcher = pattern.matcher((CharSequence) str[i]);
boolean result = matcher.matches();
if (result)
dlist.add(Double.parseDouble(str[i]));


public void run()

try
Thread.sleep(300);
catch(InterruptedException e)

e.printStackTrace();

if(dlist.size()!=(0))

m=Collections.min(dlist);
System.out.println("最小整数为:"+m);

else
System.out.println("最小整数为:没有整数");



//两个相差最大的整数
class dif implements Runnable

double min=0;
double max=0;
double bet=0;
List<Double> dlist=new ArrayList<Double>();
public dif(String d)

String[] str = d.split(",");
for(int i = 0; i < str.length; i++)

Pattern pattern = Pattern.compile("[0-9]+");
Matcher matcher = pattern.matcher((CharSequence) str[i]);
boolean result = matcher.matches();
if (result)
dlist.add(Double.parseDouble(str[i]));


public void run()

try
Thread.sleep(500);
catch(InterruptedException e)

e.printStackTrace();

if(dlist.size()!=(0) && dlist.size()!=(1))

min=Collections.min(dlist);
max=Collections.max(dlist);
bet=max-min;
System.out.println("最小整数和最大整数相差:"+bet);

else

System.out.println("最小整数和最大整数相差:整数少于2个无法计算");


参考技术A a little easy 哦。

java输入5个数输出最大和最小值

java输入5个数输出最大和最小值 不需要异常处理 尽量简洁点

可以使用数组接收所输入的数字,然后使用数组集合的排序方法Arrays.sort();进行有序排序,取出 0 索引和 length-1 索引的数值,也就是最小值和最大值。

示例代码:

import java.util.Arrays;
import java.util.Scanner;

public class Main
public static void main(String[] args)
Scanner sc = new Scanner(System.in);
int[] arr = new int[5];
try
for(int i = 0; i < arr.length; i++)
System.out.println("请输入第"+(i+1)+"个数!");
arr[i] = sc.nextInt();

Arrays.sort(arr);
System.out.println("最小值:"+arr[0]+"\n最大值:"+arr[arr.length-1]);
catch (Exception e)
throw new RuntimeException("您输入不是数字!");
finally
sc.close();


参考技术A //应该够简洁吧,无异常
import java.util.Scanner;
public class Test
static public void main(String 参数[])
Scanner s=new Scanner(System.in);
System.out.println("输入五个数字");
int max=0,min=0;
for(int i=0;i<5;i++)
int a=s.nextInt();
max=i==0?a:(a>max?a:max);
min=i==0?a:(a<min?a:min);

System.out.format("最大值:%d, 最小值:%d",max,min);


输入五个数字
20 1 4 8 2
最大值:20, 最小值:1本回答被提问者采纳
参考技术B package com.sinosoft.question;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Math

public static void main(String[] args) throws IOException
//1 接受输入
System.out.println("请输入五个数字:>");
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String str = stdin.readLine();
String[] strSplit =str.split(",");
//2 把字符转成数字
int[] numSplit = new int[strSplit.length];
for (int i = 0; i < numSplit.length; i++)
numSplit[i] = Integer.parseInt(strSplit[i]);


//1 最大数字
int max = numSplit[0];
//2 最小数字
int min = numSplit[0];
//3 加和
int sum = 0;
for (int i = 0; i < numSplit.length; i++)
if(numSplit[i]>max)
max = numSplit[i];

if(numSplit[i]<min)
min = numSplit[i];

sum = sum + numSplit[i];

System.out.println("最大数字是 > "+max+"\t最小数字是 > "+min+"\t加和等于> "+sum);


参考技术C import java.util.Scanner;
public class paixu
public static void main(String[] args)
Scanner sc=new Scanner(System.in);
int[] a=new int[5];
for(int i=0;i<a.length;i++)
System.out.println("请输入"+a.length+"个数字,第"+(i+1)+"个:");
a[i]=sc.nextInt();

System.out.print("排序前数组: ");
for(int i:a)
System.out.print(i+"\t");

System.out.println();
new paixu().f(a);

public void f(int[] a)
for(int i=a.length-1;i>0;i--)
for(int j=0;j<i;j++)
if(a[i]<a[j])
int temp=a[i];;
a[i]=a[j];
a[j]=temp;



System.out.print("排序后的数组: ");
for(int i:a)
System.out.print(i+"\t");




//排序完了想要什么样的数输出在数组中的位置即可。
参考技术D import java.util.Scanner;
class gt

public static void main(String[] args)
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int[] a=new int[5];
int <a href="https://www.baidu.com/s?wd=max&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9PWbdPAuhnvDYnHT3ryf30ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1DLrHRvnWf3PHmknWnkn1TvnWT" target="_blank" class="baidu-highlight">max</a>=0,<a href="https://www.baidu.com/s?wd=min&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9PWbdPAuhnvDYnHT3ryf30ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1DLrHRvnWf3PHmknWnkn1TvnWT" target="_blank" class="baidu-highlight">min</a>=0;
System.out.print("输入五个整数:");
for(int i = 0 ;i<5;i++)

a[i]=input.nextInt();

<a href="https://www.baidu.com/s?wd=min&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9PWbdPAuhnvDYnHT3ryf30ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1DLrHRvnWf3PHmknWnkn1TvnWT" target="_blank" class="baidu-highlight">min</a> = <a href="https://www.baidu.com/s?wd=max&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9PWbdPAuhnvDYnHT3ryf30ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1DLrHRvnWf3PHmknWnkn1TvnWT" target="_blank" class="baidu-highlight">max</a> = a[4];
for(int i=0;i<4;i++)

if(a[i] > <a href="https://www.baidu.com/s?wd=max&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9PWbdPAuhnvDYnHT3ryf30ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1DLrHRvnWf3PHmknWnkn1TvnWT" target="_blank" class="baidu-highlight">max</a>)
max=a[i];

System.out.println("最大的整数位:"+max);
for(int j=0;j<4;j++)

if(<a href="https://www.baidu.com/s?wd=min&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9PWbdPAuhnvDYnHT3ryf30ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1DLrHRvnWf3PHmknWnkn1TvnWT" target="_blank" class="baidu-highlight">min</a> > a[j])
min=a[j];

System.out.println("最小的整数位:"+min);

以上是关于Java编程、输入数字个数、平均数、最小值、最大值减去最小值、的主要内容,如果未能解决你的问题,请参考以下文章

C语言编程:从键盘输入10个数,求最大值,最小值和平均值

计算用户在Java中输入的一系列数字的总和、算术平均值、最小值和最大值? [关闭]

编程接受用户输入的若干整数,一-1标志输入的结束,求出其中的最大值,最小值和平均值

C语言编程题求代码,不会做啊

c语言:输入两个整数,计算并输出这两个整数的和·平均数·最大值·最小值?

c语言:输入两个整数,计算并输出这两个整数的和·平均数·最大值·最小值?