Results 1 to 6 of 6
  1. #1
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

    Default What's going on with my code?

    Hi! Sorry to bother everyone again, I was hoping someone can correct the error I am getting with my java code? I really want to do well in my college module but find java programming tough. The programme is trying to find the highest number in a four-digit number. However, I keep getting a compilation error " error: bad operand types for binary operator '&&' if (Digit1 > Digit2 && Digit3 && Digit4) first type:boolean second type:int
    Java Code:
    import java.util.Scanner;
    public class Digit
    {
    public static void main(String[]args)
    {
    int Num;
    Scanner in = new Scanner(System.in);
    System.out.print("Enter a four digit number: ");
    Num = Integer.parseInt(in.nextLine());
    int Digit1 = (Num / 1000);
    int Digit2 = ((Num % 1000) / 100);
    int Digit3 = (((Num % 1000) % 100) / 10);
    int Digit4 = ((((Num % 1000) % 100) % 10) / 1);
    if (Digit1 > Digit2 && Digit3 && Digit4)
    {
    System.out.print((Digit1) + " is greater than " + (Digit2) + " and " + (Digit3) + " and " + (Digit4) + ".");
    }
    else if (Digit2 > Digit1 && Digit3 && Digit4)
    {
    System.out.print((Digit2) + " is greater than " + (Digit1) + " and " + (Digit3) + " and " + (Digit4) + ".");
    }
    else if (Digit3 > Digit1 && Digit2 && Digit4)
    {
    System.out.print((Digit3) + " is greater than " + (Digit1) + " and " + (Digit2) + " and " + (Digit4) + ".");
    }
    else if (Digit4 > Digit1 && Digit2 && Digit3)
    {
    System.out.print((Digit4) + " is greater than " + (Digit1)+ " and " + (Digit2) + " and " + (Digit3) + ".");
    }
    }
    }

  2. #2
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: What's going on with my code?

    I cant read this when you don't follow forum rules.

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default Re: What's going on with my code?

    if (Digit1 > Digit2 && Digit3 && Digit4) {
    doesn't work at all!
    You have to do:
    if (Digit1 > Digit2 && Digit1 > Digit3 && Digit1 > Digit4){

  4. #4
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

    Default Re: What's going on with my code?

    Quote Originally Posted by Reeling View Post
    I cant read this when you don't follow forum rules.
    Yeah, but I no know how to code it on this forum properly :D

  5. #5
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

    Default Re: What's going on with my code?

    Thank you :)

  6. #6
    PrimerPlus is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Default Re: What's going on with my code?

    I think he wants you to put [ code] at the beginning of your code and then at the end put [ /code]
    This will allow us to read your code on the forum easier. So edit your first post and add that in.
    Make sure to not put space I just did that so you can see.

Similar Threads

  1. Replies: 1
    Last Post: 03-25-2012, 02:40 AM
  2. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  3. Replies: 3
    Last Post: 08-10-2011, 09:17 AM
  4. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •