需要在java中找到最多三个数字

use*_*357 10 java max

可能重复:
使用不同的数据类型在Java中查找最多3个数字(基本Java)

编写一个程序,使用扫描仪读取三个整数(正数)显示最多三个.(请在不使用任何运算符的情况下完成.&&或者||.这些运算符将很快在类中提及.不需要类似的循环.)

Some sample run: 

Please input 3 integers: 5 8 3
The max of three is: 8

Please input 3 integers: 5 3 1
The max of three is 5

import java.lang.Math;
import java.util.Scanner;
public class max {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please input 3 integers: ");
        String x = keyboard.nextLine();
        String y = keyboard.nextLine();
        String z = keyboard.nextLine();
        int max = Math.max(x,y,z);
        System.out.println(x + " " + y + " "+z);
        System.out.println("The max of three is: " + max);
    }
}   
Run Code Online (Sandbox Code Playgroud)

我想知道这段代码有什么问题,以及当我输入3个不同的值时如何找到最大值.

Yog*_*ngh 27

两件事情:改变变量x, y, z因为int并调用方法Math.max(Math.max(x,y),z)为只接受两个参数.

在摘要中,更改如下:

    String x = keyboard.nextLine();
    String y = keyboard.nextLine();
    String z = keyboard.nextLine();
    int max = Math.max(x,y,z);
Run Code Online (Sandbox Code Playgroud)

    int x = keyboard.nextInt();
    int y = keyboard.nextInt();
    int z = keyboard.nextInt();
    int max =  Math.max(Math.max(x,y),z);
Run Code Online (Sandbox Code Playgroud)


Lol*_*olo 2

如果您提供您所看到的错误,将会有所帮助。查看http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html,您将看到 max 只返回两个数字之间的最大值,因此您的代码可能甚至没有编译。

首先解决所有编译错误。

然后你的作业将包括通过比较前两个数字来找到三个数字的最大值,并将该最大值结果与第三个值进行比较。现在你应该已经足够找到答案了。