五个数中的最大值

huangapple go评论84阅读模式
英文:

The Biggest of Five Numbers

问题

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
       
    int a = sc.nextInt();
    int b = sc.nextInt();
    int c = sc.nextInt();
    int d = sc.nextInt();
    int e = sc.nextInt();

    if ((a >= b) && (a >= c) && (a >= d) && (a >= e)) {
        System.out.println(a);
    } else if ((b >= c) && (b >= d) && (b >= e)) {
        System.out.println(b);
    } else if ((c >= d) && (c >= e)) {
        System.out.println(c);
    } else if (d >= e) {
        System.out.println(d);
    } else {
        System.out.println(e);
    }
}
英文:

I have a problem with my homework. My program has to work for integer and floating-point numbers.

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
       
    int a=sc.nextInt();
    int b=sc.nextInt();
    int c=sc.nextInt();
    int d=sc.nextInt();
    int e=sc.nextInt();

    if ((a >= b) && (a >= c) && (a >= d) && (a >= e)) { // a >= b,c,d,e
        System.out.println (a);
    } else if ((b >= c) && (b >= d) && (b >= e)) {      // b >= c,d,e
        System.out.println ( b);
    } else if ((c >= d) && (c >= e)) {                  // c >= d,e
        System.out.println ( c);
    } else if (d >= e) {                                // d >= e
        System.out.println ( d);
    } else {                                            // e > d
        System.out.println (e);
    }
}

What's wrong with the code?

答案1

得分: 1

如果它需要适用于 int "浮点数" ,那么您应该对所有五个值使用 Scannet.nextDouble() (它们应该是 double )。也就是说,int 没有 任何 浮点数部分。类似这样,

Scanner sc = new Scanner(System.in);
double a = sc.nextDouble(), b = sc.nextDouble(), c = sc.nextDouble(),
		d = sc.nextDouble(), e = sc.nextDouble();
System.out.println(Math.max(Math.max(Math.max(Math.max(a, b), c), d), e));
英文:

If it has to work for int and "floating-point" numbers, then you should use Scannet.nextDouble() for all five values (and they should be double). That is, int has no floating point component. Something like,

Scanner sc = new Scanner(System.in);
double a = sc.nextDouble(), b = sc.nextDouble(), c = sc.nextDouble(),
		d = sc.nextDouble(), e = sc.nextDouble();
System.out.println(Math.max(Math.max(Math.max(Math.max(a, b), c), d), e));

答案2

得分: 0

你可以使用 Collections 类中的 List。不确定这是否符合您的任务要求。

public static void main(String [] args)
{
 Scanner sc = new Scanner(System.in);
 List<Double> nums = new ArrayList<>();

 for(int x = 0; x < 5; x++)
 {
  nums.add(sc.nextDouble());
 }
  
 System.out.println("输入的最大数是 " + Collections.max(nums));
}
英文:

You could use a List with the Collections class. Not sure if this falls within the parameters of your assignment though.

public static void main(String [] args)
{
 Scanner sc = new Scanner(System.in);
 List&lt;Double&gt; nums = new ArrayList&lt;&gt;();

 for(int x = 0; x &lt; 5; x++)
 {
  nums.add(sc.nextDouble());
 }
  
 System.out.println(&quot;The biggest number entered is &quot; + Collections.max(nums));
}

huangapple
  • 本文由 发表于 2020年4月7日 22:51:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/61082863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定