gpt4 book ai didi

java - 打印给定数字中的最大数字 - Java

转载 作者:太空宇宙 更新时间:2023-11-03 12:02:48 25 4
gpt4 key购买 nike

首先,如果我的问题不太清楚,我会道歉。

我希望输出是用户输入的最大可能数字。示例:

input: x = 0; y = 9; z = 5;
output: 950

我尝试了类似下面的代码。

import java.util.Scanner;

class LargestOfThreeNumbers{
public static void main(String args[]){
int x, y, z;
System.out.println("Enter three integers ");
Scanner in = new Scanner(System.in);

x = in.nextInt();
y = in.nextInt();
z = in.nextInt();

if ( x > y && x > z )
System.out.println("First number is largest.");
else if ( y > x && y > z )
System.out.println("Second number is largest.");
else if ( z > x && z > y )
System.out.println("Third number is largest.");
}
}

上面的代码会打印类似这样的内容:The seconde number is largest。这是我定义条件语句的正确方式。但是如何得到 950 作为最终结果呢?我知道这里需要一些逻辑,但我的大脑似乎无法产生它。

感谢您的帮助。

最佳答案

你可以这样做来按顺序打印数字:

// make an array of type integer
int[] arrayOfInt = new int[]{x,y,z};
// use the default sort to sort the array
Arrays.sort(arrayOfInt);
// loop backwards since it sorts in ascending order
for (int i = 2; i > -1; i--) {
System.out.print(arrayOfInt[i]);
}

关于java - 打印给定数字中的最大数字 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51052073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com