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
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.
Re: help with ' simple if ' program
Quote:
Originally Posted by
thenamesake
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:
Code:
if student belongs to sports category
display marks+bonus marks
else
display marks
This almost looks like Java ...
kind regards,
Jos
Re: help with ' simple if ' program
Quote:
Originally Posted by
PhHein
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;
}
}
}
}
Re: help with ' simple if ' program
Quote:
Originally Posted by
JosAH
You're almost there already; have a look at the translation of the above:
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;
}
}
}
}
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.
Re: help with ' simple if ' program
Quote:
Originally Posted by
thenamesake
thanks but i dont want to use ' else '
Why not?
kind regards,
Jos
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);:D:
Re: help with ' simple if ' program
Quote:
Originally Posted by
JosAH
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
Re: help with ' simple if ' program
Quote:
Originally Posted by
thenamesake
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:
Code:
if (C)
S1;
else
S2;
can be converted to 'simple' if statement(s):
Code:
boolean elsePart= true;
if (C) {
S;
elsePart= false;
}
if (elsePart)
S2;
kind regards,
Jos