What’s the highest number. Can’t figure out what’s missing?

Asked

Viewed 43 times

0

I’m doing some loop-for exercises to apply what I’ve learned. In this classic exercise request 10 numbers for the user and show at the end which is the largest. I did so:

import java.util.Scanner;

public class TheBiggerNumber{
    public static void main (String[] args){

        Double[] numbers = new Double[10];
        Double theBigNumber = 1.0;
        Double lessNumber = 0.0;
        Double comparativeNumber = 0.5;
        Scanner in = new Scanner(System.in);

        for (int i=0; i<10; i++){
            System.out.println("Write 10 numbers randomly");
            numbers[i] = in.nextDouble();
            numbers[i] = comparativeNumber;
                if ( comparativeNumber > theBigNumber) {
                    theBigNumber = numbers[i];
                } else {
                        lessNumber = numbers[i];
                }
        }
        System.out.println("The Bigger number is: " + theBigNumber );
    } //end method main
} //end class

The program compiles and runs, but the result does not increase theBigNumber, the largest number is always what I declared for theBigNumber.

Can someone help me?

  • The objective of the exercise is to show the larger of 10 numbers typed only? Da para fazer sem utilizar 2/3 do esse codigo ai.

1 answer

0

The problem is on the line

numbers[i] = comparativeNumber;

You are assigning the value 0.5 to the number at the i position of the vector and not assigning to comparativeNumber the i-th position. Like 0.5 < 1, the value of theBigNumber never changes.

  • Thanks Yan. Solved.

  • @Salatielaizza if the answer has solved your problem, click the blue V next to it to mark as accepted and solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.