P
P
PRAIT2019-06-07 12:53:09
Java
PRAIT, 2019-06-07 12:53:09

Why doesn't the first assignment work in an if?

There is a code

package code;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        try (Scanner input = new Scanner(System.in)) {
            int a;
            int b;
            int c;
            int max;
            System.out.println("Iput A");
            a = input.nextInt();
            System.out.println("Iput B");
            b = input.nextInt();
            System.out.println("Iput C");
            c = input.nextInt();

            max = a;
            if (b > max) {
                max = b;
            }
            if (c > max) {
                max = c;
            }

            System.out.println("Max = " + max);
        }
    }
}

In principle, it is very clear, but I don’t understand why the program will not work if we allow the variable and also place it in if?
if(a > max) {
          max = a;
          }       

            if (b > max) {
                max = b;
            }
            if (c > max) {
                max = c;
            }

Why does the program give an error in this case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2019-06-07
@GavriKos

And you carefully read the error - everything is written in it.
Well, or just think that in this case, println should output if none of the ifs worked.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question