「java三个数找最大」java比较三个数最大值

博主:adminadmin 2023-01-25 01:30:14 474

本篇文章给大家谈谈java三个数找最大,以及java比较三个数最大值对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用java程序写出一个程序求3个数中的最大值

int a=1,b=2,c=3 简写:int maxNum= ab?ac?a:c:bc?b:c;

System.out.print(maxNum); //输出最大值

也可以用程序判断

if(ab){

if(ac){

Ststem.out.print(a);//最大值

}else{

Ststem.out.print(c);//最大值

}

}else{

if(bc){

Ststem.out.print(b);//最大值

}else{

Ststem.out.print(c);//最大值

}

}

上面的逻辑有点乱,初学者一般这么写。还可以将三个数放到一个数组中,进行排序,排序方法很多。

用java写一个方法实现求三个数的最大值

float Max(a,b,c)

{

float m

m=ab?(ac?a:c):(bc?b:c)

//即先比较a和b的大小,如果ab,接着比较a和c的大小,如果ac,那么最大值为a,如果a=c,那么最大值为c;

如果a=b,接着比较b和c的大小,如果bc,那么最大值为b,否则最大值为c;

}

编写一个JAVA小程序,从键盘输入3个数,求这三个数的最大值。

可以使用数组,来存储输入的3个数字,然后排序,得到最大值

也可以直接一边输出一边判断,参考代码如下

import java.util.Scanner;

public class Exam {

public static void main(String[] args) {

int len =3;

System.out.println("请输入"+len+"个数字");

Scanner sc = new Scanner(System.in);

int max = sc.nextInt();//假设第一次输入的数字是最大,存在这里

for (int i = 0; i len-1; i++) {

int x = sc.nextInt();

if(xmax){//如果比max还要大.那么就替换掉max的值

max = x;

}

}

System.out.println("最大的数字是"+max);

}

}

测试

请输入3个数字

2

1

6

最大的数字是6

Java中能求出任意3个数字中最大值的代码吗?

Java中能求出任意3个数字中最大值的代码

1、if语句嵌套

2、if语句

3、if语句(假定a最大,b,c与a比较,如果比a大,则赋值给max)

4、三元运算符

5、if语句 + 逻辑运算符 (a,b,c三个数,如果不是a最大,或者b最大,就是c最大)

一、if语句嵌套

int a = 10;

int b = 30;

int c = 20;

int max;

if (a b) {

if (a c) {

max = a;

} else {

max = c;

}

} else {

if (b c) {

max = b;

} else {

max = c;

}

}

二、if语句

int a = 10;

int b = 30;

int c = 20;

int max;

if (a b) {

max = a;

} else {

max = b;

}

if (max c) {

max = c;

}

三、if语句(假定a最大,b,c与a比较,如果比a大,则赋值给max)

int a = 10;

int b = 30;

int c = 20;

int max = a;

if (b max) {

max = b;

}

if (c max) {

max = c;

}

四、三元运算符

int a = 10;

int b = 30;

int c = 20;

int max = (a b) ? a : b;

max = (max c) ? max : c;

或者

int max = ((a b ? a : b) c) ? (a b ? a : b) : c;(建议不用这种)

五、if语句 + 逻辑运算符 (a,b,c三个数,如果不是a最大,或者b最大,就是c最大)

int a = 10;

int b = 30;

int c = 20;

int max;

if (a b a c) {

max = a;

} else if (c a c b) {

max = c;

} else

max = b;

JAVA 求输入的三个整数的最大值

简单实现代码如下:

import java.util.Arrays;

import java.util.Scanner;

public class MaxOf3_2 {

/*

* 获取最大的整数

*/

public static int getMaxNum(int...a){

Arrays.sort(a);

int maxNum = a[a.length-1];

return maxNum;

}

}

java中找出三个数最大的!

//判断三个数中的大数输出

//2010-9-16

import java.util.Scanner;

public class ThreeBig {

public static void main(String [] args){

Scanner input = new Scanner(System.in); //扫描器

int one,two,three; //三个数

//输入三个数,输出

System.out.print("请输入第1个数 :");

one = input.nextInt();

System.out.print("请输入第2个数 :");

two = input.nextInt();

System.out.print("请输入第3个数 :");

three = input.nextInt();

//方法一

// int Big = one;

//

// if(Big two)

// Big = two;

// if(Big three)

// Big = three;

//

// System.out.println("\n最大数为" + Big);

//方法二

// if( one two ){

// if( one three )

// System.out.println("\n最大数为大" + one );

// else

// System.out.println("\n最大数为" + three );

// }else{

// if( two three )

// System.out.println("\n最大数为" + two );

// else

// System.out.println("\n最大数为" + three );

// }

//

//方法三

// if( onetwoonethree ){

// System.out.println("最大数为 :" + one);

// }else if(twothree){

// System.out.println("最大数为 :" + two);

// }else{

// System.out.println("最大数为 :" + three);

// }

//方法四,加排序过程

int x = one;//转换变量

if( one two ){

one = two;

two = x;

}

if( three one )

System.out.println("三个数 这样排序 : " + three + "\t" + one + "\t" + two);

else if(three two )

System.out.println("三个数 这样排序 :" + one + "\t" + two + "\t" + three);

else

System.out.println("三个数 这样排序 :" + one + "\t" + three + "\t" + two);

}

}

关于java三个数找最大和java比较三个数最大值的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。