Adblock breaks this site

Homework Help

Discussion in 'Programming General' started by Kuwi, Nov 11, 2009.

  1. Kuwi

    Kuwi Member
    Banned

    Joined:
    Jul 31, 2007
    Posts:
    35
    Referrals:
    0
    Sythe Gold:
    0
    Homework Help

    What is wrong with the following code fragment? Rewrite it so that it produces correct output.

    Code:
    if (total == MAX)
         if (total < sum)
              System.out.println("total == MAX and is < sum.");
    else
         System.out.println("total is not equal to MAX");
    
    I'm new to programming and I'm not very good with logic.
     
  2. Y0u_G0t_Pwn3d

    Y0u_G0t_Pwn3d Guru

    Joined:
    Feb 11, 2007
    Posts:
    1,478
    Referrals:
    0
    Sythe Gold:
    0
    Homework Help

    Code:
    if (total == MAX)
         if (total < sum)
              System.out.println("total == "
                                            + MAX +  " and is < " + sum);
    else
         System.out.println("total is not equal to MAX");
    I think is what you are looking for possibly....
     
  3. WCCobra

    WCCobra Newcomer

    Joined:
    Oct 7, 2009
    Posts:
    7
    Referrals:
    0
    Sythe Gold:
    0
    Homework Help

    brackets are messed up... the ELSE applies to the second IF, not the first (as it should).

    Current
    Code:
    if (total == MAX)
    {
         if (total < sum)
         {
              System.out.println("total == MAX and is < sum.");
         }
         else
         {
              System.out.println("total is not equal to MAX");
         }
    }
    

    Correct
    Code:
    if (total == MAX)
    {
         if (total < sum)
         {
              System.out.println("total == MAX and is < sum.");
         }
    }
    else
    {
         System.out.println("total is not equal to MAX");
    }
    
    You can also change the error message, but I don't think that's what they're looking for.
     
  4. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    Homework Help

    Code:
    if (total == MAX && total < sum)
              System.out.println("total == MAX and is < sum.");
    else
         System.out.println("total is not equal to MAX");
     
  5. pkwithpink

    pkwithpink Apprentice
    Banned

    Joined:
    Nov 14, 2008
    Posts:
    770
    Referrals:
    0
    Sythe Gold:
    0
    Homework Help

    Going off of Nullware's post


    This will compare the values and then also print the values out.

    Code:
    if (total == MAX && total < sum)
              System.out.println("total = " + MAX " and is < " + sum  +".");
    else
         System.out.println("Total is not equal to MAX");
     
< cd tray open n closer | Crappy Tic-Tac-Toe Game >


 
 
Adblock breaks this site