JAVA - 最高和最低数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18839041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:32:55  来源:igfitidea点击:

JAVA - Highest and Lowest numbers

java

提问by Insane

Please help me in finding the Highest and Lowest number.

请帮我找到最高和最低的数字。

I swear, I did read countless posts on the site about this problem before I decided to post the code here, because all of them had known numbers of input.

我发誓,在我决定在这里发布代码之前,我确实在网站上阅读了无数关于这个问题的帖子,因为他们所有人都知道输入的数量。

I have "unknown" input from the user. It could be any given positive or negative numbers. I am totally blank now after trying it for almost a day. Any helps is Greatly Appreciated.

我有来自用户的“未知”输入。它可以是任何给定的正数或负数。在尝试了将近一天后,我现在完全空白。任何帮助都非常感谢。

    package test;

import java.util.Scanner;
import java.lang.*;
import java.io.*;

public class Test {

    public static void main(String[] args){

int highest=Integer.MIN_VALUE;
int lowest=Integer.MAX_VALUE;

Scanner in = new Scanner(System.in);

System.out.println("Enter any positive or negative number");
int n = in.nextInt();

if(n > highest){
    lowest = highest;
    highest = n;
}

if (n < lowest){
    highest=lowest;
    lowest=n;
}

System.out.println(highest);
System.out.println(lowest);


    }
}

回答by Willem Van Onsem

  1. Why are you saving the lowestvalue in the highestvalue?
  2. You should define a loop
  3. Complete code:

    Scanner in = new Scanner(System.in);
    while(in.hasNextInt()) {
      System.out.println("Enter any positive or negative number");
      int n = in.nextInt();
      if(n > highest){
          highest = n;
      }
      if (n < lowest){
          highest=lowest;
          lowest=n;
      }
    }
    System.out.println(highest);
    System.out.println(lowest);
    
  1. 你为什么要保存lowest值中的highest值?
  2. 你应该定义一个循环
  3. 完整代码:

    Scanner in = new Scanner(System.in);
    while(in.hasNextInt()) {
      System.out.println("Enter any positive or negative number");
      int n = in.nextInt();
      if(n > highest){
          highest = n;
      }
      if (n < lowest){
          highest=lowest;
          lowest=n;
      }
    }
    System.out.println(highest);
    System.out.println(lowest);
    

回答by progrenhard

This is where your code is breaking.

这是您的代码中断的地方。

highest=lowest;
lowest=n;

Now about initializing these variables.

现在关于初始化这些变量。

How I, personally would start this program would be initializing both the lowest and highest with n.So you don't have to worry any bounds.

我个人如何启动这个程序将初始化最低和最高,n.所以你不必担心任何界限。

System.out.println("Enter any positive or negative number");
int n = in.nextInt();
int highest = n;
int lowest = n;

for(int i = 0; i < 9; i++){
    System.out.println("Enter any positive or negative number");
    n = in.nextInt();
    if(n > highest){
        highest = n;
    }else if (n < lowest){
        lowest=n;
    }
}

System.out.println(highest);
System.out.println(lowest);

This asks the user to input 10 different digits. then prints out the highest and lowest values.

这要求用户输入 10 个不同的数字。然后打印出最高和最低值。

else if (n < lowest)this could also be a else ifbecause if nis the highest value it cant be also the lowest value because of my initialization at the start.

else if (n < lowest)这也可能是else if因为如果n是最高值,它也不能是最低值,因为我在开始时进行了初始化。