在for循环中寻找最大值

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

Finding Max value in for Loop

问题

以下是您要翻译的内容:

我是Java的初学者,只想知道如何从在for循环中声明的整数中找到最大和最小值(使用扫描器获取用户输入)。此程序从一个名为Car的类创建对象,并获取有关名称、注册、颜色和行程次数的信息。

行程次数提示for循环从0(里程表的初始读数)打印输出到变量carSample.numberOfTrips指定的任何值的里程表读数。

我尝试了声明一个新变量;int maximum = carSample.odometerReading.MAX_VALUE;(然后将其打印出来)
以及最小值,但都没有成功;我收到以下错误:

TestCar.java:25:错误:无法对int进行解引用
int maximum = carSample.odometerReading.MAX_VALUE;

public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    car carSample = new car(); // 创建Car类的对象

    carSample.name = input.nextLine();
    carSample.registration = input.nextLine();
    carSample.colour = input.nextLine();
    carSample.numberOfTrips = input.nextInt();

    for (int i = 0; i < carSample.numberOfTrips; i++) {
        System.out.print("Odometer reading " + (i) + ": ");
        int odometerReading = input.nextInt();
    }
}

非常感谢您的帮助和洞察力,非常感谢您的时间!

英文:

I'm a beginner at java and was just wondering how to find the max and minimum value from integers declared in a for loop (using a scanner to obtain user input) this program creates an object from a class Car and obtains information on the name, registration, colour and number of trips.

The number of trips prompts the for loop to print out odometer readings from 0 (odometer's initial reading) to whatever the variable carSample.numberOfTrips specifies.

I've tried declaring a new variable; int maximum = carSample.odometerReading.MAX_VALUE; (Then printing it)
aswell with minimum, however to no success; I receive the following error:

TestCar.java:25: error: int cannot be dereferenced
int maximum = carSample.odometerReading.MAX_VALUE;

public static void main(String[] args){
Scanner input = new Scanner(System.in);
    car carSample = new car(); // Creates object of class Car

carSample.name = input.nextLine();
carSample.registration = input.nextLine();
carSample.colour = input.nextLine();
carSample.numberOfTrips = input.nextInt();

for (int i = 0; i &lt; carSample.numberOfTrips; i++) {
    System.out.print(&quot;Odometer reading &quot; + (i) + &quot;: &quot;);
    int odometerReading = input.nextInt();
}

Any help or insight into how this can be peformed is really appreciated, thank you for your time!

答案1

得分: 3

int maximum = Integer.MIN_VALUE;
for (int i = 0; i < carSample.numberOfTrips; i++) {
    System.out.print("行驶里程 " + (i) + ":");
    int odometerReading = input.nextInt();
    if (odometerReading > maximum) {
        maximum = odometerReading;
    }
}

System.out.println(maximum); // 最大值
英文:
int maximum = Integer.MIN_VALUE;
for (int i = 0; i &lt; carSample.numberOfTrips; i++) {
    System.out.print(&quot;Odometer reading &quot; + (i) + &quot;: &quot;);
    int odometerReading = input.nextInt();
    if (odometerReading &gt; maximum) {
        maximum = odometerReading;
    }
}

System.out.println(maximum); // Maximum value

答案2

得分: 2

如果读数例如为 20 10 22 41 11

首先处理第一个读数:20

然后将该值用作您的最大值和最小值。因此,最大值 = 20,最小值 = 20。

然后对于其他读数,将读数与当前的最大值和最小值进行比较,并相应地更新最大值和最小值。
例如:第二个读数:10。10 小于当前最大值 20,所以最大值不变。10 小于当前最小值 20,因此将最小值更新为 10... 然后继续对循环的剩余部分进行类似操作。

英文:

If readings are for example 20 10 22 41 11

Do the first reading. 20

And use that value as your maximum and also minimum. So maximum = 20 and minimum = 20.

Then for the other readings, compare the reading to the current maximum and minimum and update the maximum and minimum accordingly.
E.g: second reading: 10. 10 is less than current maximum of 20, so no change in maximum. 10 is less than current minimum of 20, so update minimum to 10....and repeat for the remainder of the loop.

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

发表评论

匿名网友

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

确定