Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: HELP WITH MY BEGINER JAVA CODE..

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HELP WITH MY BEGINER JAVA CODE..

    // I created a simple java code which displays students grade according to the marks they got. There is no compilation error, but all grades are displayed as D. So please help me how can i make it to display correctly.



    // The code is as follows.





    class grade {
    	public static void main (String[]arguments) {
     
    	int totalmarks = 400;
    	int marks = 0;
    	String grade;
     
     
     
    	if (marks < 100)
    	grade = "D";
     
    	else if	(marks < 200)
    	grade = "C";
     
     
    	else if	(marks < 300)
    	grade = "B";
     
    	else if (marks <= 400)
    	grade = "A";
     
     
     
     
     
     
    	// student1 got 400 marks.
    	marks = 400;
    	System.out.println("marks scored by student1 are " + marks);
    	System.out.println(grade);
     
     
    	// student2 got 352 marks.
    	marks = 352;
    	System.out.println("marks scored by student2 are " + marks);
    	System.out.println(grade);
     
    	//student3 got 300 marks.
    	marks = 300;
    	System.out.println("marks scored by student3 are " + marks);
    	System.out.println(grade);
     
    	//student4 got 253 marks.
    	marks = 253;
    	System.out.println("marks scored by student4 are " + marks);
    	System.out.println(grade);
     
    	//student5 got 200 marks.
    	marks = 200;
    	System.out.println("marks scored by student5 are " + marks);
    	System.out.println(grade);
     
    	//student6 got 150 marks.
    	marks = 150;
    	System.out.println("marks scored by student6 are" + marks);
    	System.out.println(grade);
     
    	//student7 got 100 marks.
    	marks = 100;
    	System.out.println("marks scored by student7 are " + marks);
    	System.out.println(grade);
     
    	//student8 got 50 marks.
    	marks = 50;
    	System.out.println("marks scored by student8 are " + marks);
    	System.out.println(grade);
     
    	//student9 got 25 marks.
    	marks = 25;
    	System.out.println("marks scored by student9 are " + marks);
    	System.out.println(grade);
     
    	//student10 got 0 marks.
    	marks = 0;
    	System.out.println("marks scored by student10 are " + marks);
    	System.out.println(grade);
     
     
    			}
    		}


    EDIT: All the part of the program thinks that the marks variable value is only equal to the value when it is initialised (= 0). Hence it shows D grade for all cases. How can i overcome this.
    Last edited by nfs1mostwanted; March 2nd, 2012 at 03:43 AM.


  2. #2
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: HELP WITH MY BEGINER JAVA CODE..

    Hi there.
    I'm no pro at java by any means, but one thing I noticed you could do would be to switch your if/else if statements to be in the opposite order, so it would check first off if your grades were high enough for an "A", and so on
    Here's what I'm talking about:
     if (marks<=400)
    {
         grade="A";
    }
    else if (marks<300)
    {
         grade="B";
    }
    else if (marks<200)
    {
         grade="C";
    }
    else if (marks<100)
    {
          grade = "D";
    }

    I'm sure someone else has a cleaner way of doing it, but I think that should work.
    Have a good one

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP WITH MY BEGINER JAVA CODE..

    Hey thanks for the reply. That didn't work, but i got something with your reply.

    When i first initialised the marks variable i gave it a value of 0. Now the entire program thinks that the marks variable value is 0. That is why it is displaying the same grade letter everytime. Even if i change marks = marks + 50 each time it still thinks that the marks value is zero.

    I dont know how to overcome this.

    If i dont initialize marks value when it is introduced im getting a compilation error that it hasnt been initialised.
    And whatever the value marks has when it is introduced, the entire program displays its corresponding grade letter.

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: HELP WITH MY BEGINER JAVA CODE..

    Quote Originally Posted by nfs1mostwanted View Post

    And whatever the value marks has when it is introduced, the entire program displays its corresponding grade letter.
    This happens because the compiler goes through the program line by line from top to bottom. So when you check for the "marks" value with your if statements it has the value of 0 and the "grade" is assigned with the corresponding value, "D". When you are printing "grade" it prints the value that was assigned before.
    Quote Originally Posted by nfs1mostwanted View Post
    I dont know how to overcome this.
    A way to fix this is to place your if statements after you give "marks" a value (not very efficient). A better way is to make a method taking the "marks" as an argument which gives "grade" the corresponding value (and prints it if you want). So you can call this method instead of
    System.out.println(grade);

    Except from the above i think you should check again your if statements. For example when "marks" is 55 all of them are true. You should better use intervals (e.g. less than 400 and greater than 300).

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: HELP WITH MY BEGINER JAVA CODE..

    if (marks>=50 && marks<=100){
             // Do this
    }else if...
    ...
    ...
    ...
    }else{
       // Do this
    }

  6. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP WITH MY BEGINER JAVA CODE..

    @andreas90 and @Mr.777
    Ya, i know how to do the same thing with marks arguments. But just trying this way.
    And if i have to place the "if statements" after giving a value to the marks then i need to type the if statement everytime i change the value of marks. This is inefficient as u told, and directly printing out grade as text will be worth than doing this.
    And i don't thing a value of 55 will satisfy the condition of all grades, because the program must process the "else statement" only when the "if condition" is not satisfied. And i have also tried reversing the order of if-else statements and giving a range for if condition, but it still didnt work.


    This below modified code still prints the grade D for all students.

    class grade {
    	public static void main (String[]arguments) {
     
     
    	int marks;
    	marks = 0;
    	String grade;
     
     
     
    	if (marks>300 && marks<=400){
    	grade = "A";
    	}
     
    	else if	(marks>200 && marks<=300){
    	grade = "B";
    	}
     
    	else if	(marks>100 && marks<=200){
    	grade = "C";
    	}
     
    	else {
    	grade = "D";
    	}
     
     
     
    	// student1 got 400 marks.
    	marks = 400;
    	System.out.println("marks scored by student1 are " + marks);
    	System.out.println(grade);
     
     
    	// student2 got 352 marks.
    	marks = 352;
    	System.out.println("marks scored by student2 are " + marks);
    	System.out.println(grade);
     
    	//student3 got 300 marks.
    	marks = 300;
    	System.out.println("marks scored by student3 are " + marks);
    	System.out.println(grade);
     
    	//student4 got 253 marks.
    	marks = 253;
    	System.out.println("marks scored by student4 are " + marks);
    	System.out.println(grade);
     
    	//student5 got 200 marks.
    	marks = 200;
    	System.out.println("marks scored by student5 are " + marks);
    	System.out.println(grade);
     
    	//student6 got 150 marks.
    	marks = 150;
    	System.out.println("marks scored by student6 are" + marks);
    	System.out.println(grade);
     
    	//student7 got 100 marks.
    	marks = 100;
    	System.out.println("marks scored by student7 are " + marks);
    	System.out.println(grade);
     
    	//student8 got 50 marks.
    	marks = 50;
    	System.out.println("marks scored by student8 are " + marks);
    	System.out.println(grade);
     
    	//student9 got 25 marks.
    	marks = 25;
    	System.out.println("marks scored by student9 are " + marks);
    	System.out.println(grade);
     
    	//student10 got 0 marks.
    	marks = 0;
    	System.out.println("marks scored by student10 are " + marks);
    	System.out.println(grade);
     
     
    			}
    		}


    Now the real question is ,
    CAN WE ASK THE JAVA COMPILER/INTERPRETER TO AUTOMATICALLY CHANGE THE VALUE OF A STRING VARIABLE DEPENDING ON THE VALUE OF ANOTHER INTEGER VARIABLE.???
    Last edited by nfs1mostwanted; March 2nd, 2012 at 12:16 PM.

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: HELP WITH MY BEGINER JAVA CODE..

    CAN WE ASK THE JAVA COMPILER/INTERPRETER TO AUTOMATICALLY CHANGE THE VALUE OF A STRING VARIABLE DEPENDING ON THE VALUE OF ANOTHER INTEGER VARIABLE.???
    No.
    And, look at your code, try thinking from the first if to the last, what's the value and why it goes for 'D'. There is problem in your logic actually.

Similar Threads

  1. Arrays (Beginer)
    By stephen6292 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 16th, 2012, 09:13 AM
  2. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  3. beginer neeeds helppppp!
    By NewAtJava in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2011, 06:33 PM
  4. Replies: 10
    Last Post: April 5th, 2011, 09:09 AM
  5. Yet another beginer help post...
    By Gondee in forum JDBC & Databases
    Replies: 3
    Last Post: November 14th, 2010, 02:31 AM