
正文
求最大值java代码 java求最大最小值
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java如何求最大值?
测试结果为
请输入第1个数
2
请输入第2个数
3
请输入第3个数
4
请输入第4个数
5
请输入第5个数
6
请输入第6个数
7
请输入第7个数
7
请输入第8个数
8
请输入第9个数
9
请输入第10个数
423
最大数为:423
import java.util.Scanner;
public class MaxNum {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int max = Integer.MIN_VALUE;
for(int i = 1; i = 10; i++) {
System.out.println("请输入第" + i + "个数");
int num = in.nextInt();
if(num = max) max = num;
}
System.out.println("最大数为:" + max);
}
}
相关问答
Q1: 给我写一个求一个数组里面的最大值的java代码,谢谢
@Test
public void test2(){
int [] arr = new int [5];
arr[0] = 6;
arr[1] = 3;
arr[2] = 2;
arr[3] = 4;
arr[4] = 9;
int max = 0 ;
for (int i : arr) {
if(i max){
max = i ;
}
}
System.out.println("最大值为:"+max);
}
最大值为:9
Q2: 编写JAVA程序,从十个数中找出最大值
import java.util.Scanner;
public class Test {
public static void main(String args[]){
int num[]=new int[10];
int max;
Scanner scan=new Scanner(System.in);
System.out.print("请输入10个数字:");
for(int i=0; i10; i++){
num[i]=scan.nextInt();
}
max=num[0];
for(int i=1; i10; i++){
if(maxnum[i]) max=num[i];
}
System.out.println("最大值是"+max);
}
}
Q3: 求java源程序代码:输入两个数,输出最大值
一下代码仅供参考
package com.kidd.test.zhidao;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = 0;
int b = 0;
boolean next = false;
while (!next) {
System.out.print("请输入两个整数(用空格分隔):");
try {
a = sc.nextInt();
b = sc.nextInt();
next = true;
} catch (NoSuchElementException e) {
System.out.println("输入有误,请重新输入.");
sc.nextLine();
continue;
}
}
System.out.printf("最大值为:%d\n", a b ? a : b);
}
}
关于求最大值java代码和java求最大最小值的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








