P
P
PRAIT2019-08-02 03:56:35
Java
PRAIT, 2019-08-02 03:56:35

Is the ternary operator used correctly?

Hello! I have a code that is written with a conditional statement.

package ru;

import java.util.Scanner;

public class Ones {

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

            max = a;
            if (b > max) {
                max = b;
            }
            if (c > max) {
                max = c;
            }
            if (d > max) {
                max = d;
            }
            System.out.println(a + ";" + b + ";" + c + ";" + d);
            System.out.println("Maximum number " + max);
        }
    }
}

I want to rewrite it to ternary operator
package ru.mail.stonap;

import java.util.Scanner;

public class ones {

  public static void main(String[] args) {
    try (Scanner sc = new Scanner(System.in)) {
      int a = 2, b = 4, c = 5, d = 7, max;
      max = a > b ? a : b; 
      max = c > d ? c : d; 
      System.out.println(max);
    }
  }
}

Please look guys, is the code formatted correctly? NetBeans underlines line 10 but doesn't write what's wrong. Thanks!!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
EVGENY T., 2019-08-02
@PRAIT

https://docs.oracle.com/javase/7/docs/api/java/lan...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question