Homework Help

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

Homework Help
  1. Unread #1 - Nov 11, 2009 at 12:01 AM
  2. Kuwi
    Joined:
    Jul 31, 2007
    Posts:
    35
    Referrals:
    0
    Sythe Gold:
    0

    Kuwi Member
    Banned

    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.
     
  3. Unread #2 - Nov 11, 2009 at 11:03 AM
  4. Y0u_G0t_Pwn3d
    Joined:
    Feb 11, 2007
    Posts:
    1,478
    Referrals:
    0
    Sythe Gold:
    0

    Y0u_G0t_Pwn3d Guru

    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....
     
  5. Unread #3 - Nov 13, 2009 at 1:11 PM
  6. WCCobra
    Joined:
    Oct 7, 2009
    Posts:
    7
    Referrals:
    0
    Sythe Gold:
    0

    WCCobra Newcomer

    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.
     
  7. Unread #4 - Nov 13, 2009 at 7:35 PM
  8. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    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");
     
  9. Unread #5 - Dec 20, 2009 at 1:40 AM
  10. pkwithpink
    Joined:
    Nov 14, 2008
    Posts:
    770
    Referrals:
    0
    Sythe Gold:
    0

    pkwithpink Apprentice
    Banned

    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 >

Users viewing this thread
1 guest


 
 
Adblock breaks this site