Calculate the smallest element of a list

Asked

Viewed 696 times

1

Given a sequence of integers (read via Scanner), determine the lower of these values.

My code:

import java.util.Scanner;

public class MenorValorSequencia {

    public static void main (String args []) {

    Scanner sc = new Scanner (System.in);
    int [] variavel = new int [10];
    for (int i = 0; i<=9; i++) {
    variavel[i] = sc.nextInt();
    }

    int menorValor = 0;
        for (int i = 0; i<=9; i++) {
            if (variavel[i]<menorValor) {
                menorValor = variavel[i];
            }
        System.out.println (menorValor(i)); // erro: não pode achar variável menorvalor
        }
    }
}

Java code for this problem can be done like this?

  • Please do not use bold in the whole text, the feature is to highlight points that are important only

  • 1

    And you are treating variable as if it were a method, menorValue is a variable, there is no such call menorValor(i). Remove the (i) in front.

  • Did any of the answers below solve your problem? Do you think you can accept one of them? Check out the [tour] how to do this, if you still don’t know how to do it. This helps the community by identifying the best solution for you. You can only accept one of them, but you can vote for any question or answer you find useful on the entire site (if you have enough score).

2 answers

6

It has three errors: the initial value must be the largest integer possible; it must have the variable printed and not a function printed; and the printing must occur after the loop ends and not inside it.

import java.util.Scanner;

class MenorValorSequencia {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int[] variavel = new int[10];
        for (int i = 0; i <= 9; i++) variavel[i] = sc.nextInt();
        int menorValor = Integer.MAX_VALUE;
        for (int i = 0; i <= 9; i++) if (variavel[i] < menorValor) menorValor = variavel[i];
        System.out.println(menorValor);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • I like the idea of using the first value read as menorValor initial.

  • @iTSangar is a good idea yes

  • When we went to do this algorithm in class I remember the teacher using this example: We need to find out which child of a family is the most beautiful, if you only have one child soon he is the most beautiful. And so it will be until you have another son to compare.

3

The initial value of int menorValor = 0; is the problem, for this value to be changed it is necessary to enter a negative value.

And here is not an array System.out.println (menorValor(i));

The right thing would be: System.out.println (menorValor);

  • Thank you very much. I fixed the code as you said, which now runs, but prints out 10 times the same correct result. Please, how to fix?

  • 1

    Your System.out.println(menorValor) this inside the for change it to after the key just after it

  • Thank you very much. As you have already noticed, I am Beginner in Java. Now executed perfectly.

Browser other questions tagged

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