0

I have a problem with my homework. My program has to work for integer and floating-point numbers.

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);

    int a=sc.nextInt();
    int b=sc.nextInt();
    int c=sc.nextInt();
    int d=sc.nextInt();
    int e=sc.nextInt();

    if ((a >= b) && (a >= c) && (a >= d) && (a >= e)) { // a >= b,c,d,e
        System.out.println (a);
    } else if ((b >= c) && (b >= d) && (b >= e)) {      // b >= c,d,e
        System.out.println ( b);
    } else if ((c >= d) && (c >= e)) {                  // c >= d,e
        System.out.println ( c);
    } else if (d >= e) {                                // d >= e
        System.out.println ( d);
    } else {                                            // e > d
        System.out.println (e);
    }
}

What's wrong with the code?

HoRn
  • 1,458
  • 5
  • 20
  • 25
  • Are you able to use an array? – ThallsEternal Apr 07 '20 at 14:54
  • I don't know how to do it for floating point and integer.examples for solution in my homework. –  Apr 07 '20 at 14:58
  • a b c d e biggest 5 2 2 4 1 5 -2 -22 1 0 0 1 -2 4 3 2 0 4 0 -2.5 0 5 5 5 -3 -0.5 -1.1 -2 -0.1 -0.1 –  Apr 07 '20 at 14:58
  • Have you seen this older message. Sounds like what you are trying to do: https://stackoverflow.com/questions/32395648/largest-5-in-array-of-10-numbers-without-sorting – Casey Harrils Apr 08 '20 at 05:37

2 Answers2

1

If it has to work for int and "floating-point" numbers, then you should use Scannet.nextDouble() for all five values (and they should be double). That is, int has no floating point component. Something like,

Scanner sc = new Scanner(System.in);
double a = sc.nextDouble(), b = sc.nextDouble(), c = sc.nextDouble(),
        d = sc.nextDouble(), e = sc.nextDouble();
System.out.println(Math.max(Math.max(Math.max(Math.max(a, b), c), d), e));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • a b c d e biggest 5 2 2 4 1 5 -2 -22 1 0 0 1 –  Apr 07 '20 at 15:01
  • this is have to be my output in console –  Apr 07 '20 at 15:02
  • the program should work regardless of whether the user insert targets integers or not –  Apr 07 '20 at 15:04
  • *My program have to work for integer and floating-point numbers* and *the program should work regardless of whether the user insert targets integers or not* and *The Biggest of Five Numbers* are the only requirements you have so far given. They are not all compatible with each other. So you need to go back, and check your instructions. And, **edit** your question instead of adding unformatted comments. – Elliott Frisch Apr 07 '20 at 15:35
0

You could use a List with the Collections class. Not sure if this falls within the parameters of your assignment though.

public static void main(String [] args)
{
 Scanner sc = new Scanner(System.in);
 List<Double> nums = new ArrayList<>();

 for(int x = 0; x < 5; x++)
 {
  nums.add(sc.nextDouble());
 }

 System.out.println("The biggest number entered is " + Collections.max(nums));
}