Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By JosAH

Thread: help with ' simple if ' program

  1. #1
    thenamesake is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default help with ' simple if ' program

    hi,
    i am new to java just started to learn i am now learning decision making logic by using simple if, if-else, and so on.
    now i am stuck at a program.

    i want display total marks of students
    if student belongs to sports category then bonus marks will added in his original marks and then his total marks will be display
    and if student is not from sports category then bonus marks will not added only his original marks will display

    what will be the syntax of this logic?
    i want to use only simple if condition in code
    thanks in advance

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: help with ' simple if ' program

    Post your code with code tags. Tell us what goes wrong, post compiler errors and runtime Exceptions. We'll gladly help, but won't provide ready made code.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: help with ' simple if ' program

    Quote Originally Posted by thenamesake View Post
    if student belongs to sports category then bonus marks will added in his original marks and then his total marks will be display
    and if student is not from sports category then bonus marks will not added only his original marks will display
    You're almost there already; have a look at the translation of the above:

    Java Code:
    if student belongs to sports category
       display marks+bonus marks
    else
       display marks
    This almost looks like Java ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    thenamesake is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: help with ' simple if ' program

    Quote Originally Posted by PhHein View Post
    Post your code with code tags. Tell us what goes wrong, post compiler errors and runtime Exceptions. We'll gladly help, but won't provide ready made code.
    here is my code

    package simpleif1;
    public class SimpleIf1
    {
    public static void main(String arg[])
    {
    int total_marks;
    int[] marks = {50, 60, 84, 40};
    int[] bonus_marks = {20, 32, 27, 25};
    {
    if(category == SPORTS)
    {
    total_marks = marks + bonus_marks;
    }
    }



    }
    }

  5. #5
    thenamesake is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: help with ' simple if ' program

    Quote Originally Posted by JosAH View Post
    You're almost there already; have a look at the translation of the above:

    Java Code:
    if student belongs to sports category
       display marks+bonus marks
    else
       display marks
    This almost looks like Java ...

    kind regards,

    Jos

    thanks but i dont want to use ' else '

    here is my code

    package simpleif1;
    public class SimpleIf1
    {
    public static void main(String arg[])
    {
    int total_marks;
    int[] marks = {50, 60, 84, 40};
    int[] bonus_marks = {20, 32, 27, 25};
    {
    if(category == SPORTS)
    {
    total_marks = marks + bonus_marks;
    }
    }



    }
    }

  6. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: help with ' simple if ' program

    Hi thenamesnake,

    Firstly, in your if statement you will need to compare to "SPORTS" not SPORTS.

    Secondly, you will need to loop through your arrays adding the content of bonus_marks to marks.
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Finally, print out the content of marks.

    Regards.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: help with ' simple if ' program

    Quote Originally Posted by thenamesake View Post
    thanks but i dont want to use ' else '
    Why not?

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: help with ' simple if ' program

    maybe this will help

    student_marks = marks;
    if(Sports)
    student_marks = student_marks + bonus_marks; // this will be skipped if there's no Sport

    sytem.out.println(student_marks);
    Last edited by Kagiso; 02-13-2013 at 12:18 PM.

  9. #9
    thenamesake is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: help with ' simple if ' program

    Quote Originally Posted by JosAH View Post
    Why not?

    kind regards,

    Jos
    it is because, i am doing exercises it strictly having this condition.
    that i must do it only with 'simple if'

    thanks

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: help with ' simple if ' program

    Quote Originally Posted by thenamesake View Post
    it is because, i am doing exercises it strictly having this condition.
    that i must do it only with 'simple if'

    thanks
    Every if-else statement:

    Java Code:
    if (C) 
       S1; 
    else 
       S2;
    can be converted to 'simple' if statement(s):

    Java Code:
    boolean elsePart= true;
    if (C) {
       S;
       elsePart= false;
    }
    if (elsePart)
       S2;
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help with simple program...
    By blink16j in forum New To Java
    Replies: 4
    Last Post: 01-15-2013, 05:59 PM
  2. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 05:12 AM
  3. Please help with simple program.. Very simple.
    By jonytek in forum New To Java
    Replies: 7
    Last Post: 02-14-2011, 12:44 AM
  4. simple program
    By blastoff in forum New To Java
    Replies: 5
    Last Post: 04-14-2010, 11:25 PM
  5. Simple Program
    By TheRocket in forum Advanced Java
    Replies: 15
    Last Post: 12-30-2008, 02:35 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
  •