展开全部
可以使用数组接收所输入的数字,然后使用数组集合的排序方法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();
}
}
}
示例代码:
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();
}
}
}
展开全部
//应该够简洁吧,无异常
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
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
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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);
}
}
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);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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");
}
}
}
//排序完了想要什么样的数输出在数组中的位置即可。
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");
}
}
}
//排序完了想要什么样的数输出在数组中的位置即可。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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);
}
}
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);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询