Results 1 to 7 of 7
  1. #1
    Ezhan's Avatar
    Ezhan is offline Member
    Join Date
    Dec 2015
    Location
    Pakistan>>>Hyderabad
    Posts
    4
    Rep Power
    0

    Default IF Statement showing wrong results

    Java Code:
    import java.util.Scanner;
    class apples{
    	public static void main(String args[]){
    
    		Scanner ez = new Scanner(System.in);
    		Double word, excel, ppt, access, obtained_Marks, total, percentage;
    
    		System.out.println("Enter Your Word Marks ");
    		word = ez.nextDouble();
    		
    		System.out.println("Enter Your Excel Marks " );
    		excel = ez.nextDouble();
    		
    		System.out.println("Enter Your Power Point Marks ");
    		ppt = ez.nextDouble();
    		
    		System.out.println("Enter Your Access Marks ");
    		access = ez.nextDouble();
    	
    		total = 400.0;
    		obtained_Marks = word+excel+ppt+access;
    		percentage = obtained_Marks/total*100;
    		
    		System.out.println("Total Marks Are: " +total);
    		System.out.println("Your Marks Are: " + obtained_Marks);
    		System.out.println("Your Percentage is: " + percentage);
    		
    		if(ppt >= 80 && ppt <= 100 ){
    			System.out.println("Your Grade is A1");
    		}
    		
    		else if(ppt >= 70 && ppt < 80){
    			System.out.println("Your Grade is A");
    		}
    		else if(ppt >= 60 && ppt < 70){
    			System.out.println("Your Grade is B");
    		}
    		else if(ppt >= 50 && ppt < 60){
    			System.out.println("Your Grade is C");
    		}
    		else if(ppt >= 40 && ppt < 50){
    			System.out.println("Your Grade is D");
    		}
    		
    		else{
    			System.out.println("You Are Fail");
    		}
    	}
    }
    My code is simple it will print "Enter Your Word Marks" and then asking excel, power point, access marks and then you put the marks then it total marks and put them in "o" variable which is obtained marks then it shows the total marks are "400" and your marks are "the marks you got in all 4 subjects" after it gives the percentage then it shows the grade.

    when i put word, excel, ppt and access marks same then the if statement works correctly and shows the correct grade, but as we all know marks are not same in all subjects they are different so when i put different marks the "if" statement dosent works correctly it gives me false grade like at 50% it gives me fail and at 38% it gives A1, (it gives correct when all the values are same) anyone have any solution of this?

    (IF) works correctly in:
    Word=70 marks
    excel=70
    ppt=70
    access=70

    but does not works correctly if we put different marks:
    Word=82 marks
    excel=69
    ppt=71
    access=44

    and we cant put same marks in all subjects as it isnt possible to get same marks in all subjects so the marks will be different, but if the "IF" statement working correctly in (same marks) then why it is giving me wrong message in (Different Maks) and idea ? i never face this problem so i have no idea.

    EDIT: I Edited the syntax and as now it shows the mistake clearly in the IF state i put the "ppt" variable which contains power point marks, there should be this variable "Percentage", and thats why i was getting false result, thanks for replies guys.
    Last edited by Ezhan; 12-22-2015 at 06:29 PM. Reason: Edited the Syntax

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: IF Statement showing wrong results

    Can you copy the console from when you execute the program and paste it here. Add some comments showing what is wrong with the output and post what output you expect.


    One MAJOR problem I see with the code is the use of single letter variable names. The name of a variable should describe what data the variable contains.
    If you give the variables proper names, the error will be evident.
    Last edited by Norm; 12-22-2015 at 05:43 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: IF Statement showing wrong results

    You need to examine your variables very carefully.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Ezhan's Avatar
    Ezhan is offline Member
    Join Date
    Dec 2015
    Location
    Pakistan>>>Hyderabad
    Posts
    4
    Rep Power
    0

    Default Re: IF Statement showing wrong results

    Oh My God, i didnt noticed one big mistake, guys thank you for helping me, your replies really helped and yes that is the problem the variables, where i have to put "perc" i putted at that place "p" that was the problem because "P" contains the power point marks and "perc" contains the percentage,

    i put the variables at wrong place, and now when i changed all the variables "w,e,p,o,t" these into their full form then i found the mistake and fixed and now it is working correctly, Thank you guys for helping i really appricate.

  5. #5
    Ezhan's Avatar
    Ezhan is offline Member
    Join Date
    Dec 2015
    Location
    Pakistan>>>Hyderabad
    Posts
    4
    Rep Power
    0

    Default Re: IF Statement showing wrong results

    i am having problem again with IF statement, this time my problem is i cant use Text in IF statement i created just a simple program using IF statement and added Text in IF statement but it always shows me the (else statmenet) even i put the correct answer it should show me IF one but it always shows me else what should i do? i dont know what is problem with my coding:

    Java Code:
    import java.util.Scanner;
    class apples{
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
    		
    		String name;
    		
    		System.out.println("Enter Your Name");
    		name = input.nextLine();
    		
    		if(name=="Ezhan"){
    			System.out.println("Wellcome Ezhan");
    		}
    		
    		else{
    			System.out.println("Error");
    		}
    		
    	}
    }
    And after this above program^^^ i modified this and changed some few things like i changed the string into int and in IF statement i set it to get numbers and it works great and but with text it dosent work this program works below, but above dosent work cause i used text in there (IF statement Does not work with Text?)

    Java Code:
    import java.util.Scanner;
    class apples{
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
    		
    		int number;
    		
    		System.out.println("Enter Your Number");
    		number = input.nextInt();
    		
    		if(number==1){
    			System.out.println("Wellcome Ezhan");
    		}
    		
    		else{
    			System.out.println("Error");
    		}
    		
    	}
    }
    Last edited by Ezhan; 12-27-2015 at 07:11 AM. Reason: Forgot To Add the 2nd program

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: IF Statement showing wrong results

    IF statement Does not work with Text?)
    The problem is with the compare operator. The == operator does not work with objects. You need to use the equals() method to compare the contents of objects like Strings. The == operator is for comparing primitive variables like int
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Ezhan's Avatar
    Ezhan is offline Member
    Join Date
    Dec 2015
    Location
    Pakistan>>>Hyderabad
    Posts
    4
    Rep Power
    0

    Default Re: IF Statement showing wrong results

    Quote Originally Posted by Norm View Post
    The problem is with the compare operator. The == operator does not work with objects. You need to use the equals() method to compare the contents of objects like Strings. The == operator is for comparing primitive variables like int
    oh, then i guess i have to learn equals() method because i didnt learn that method, hope ill learn about that method in my tution center, its closed for few days so i thought to make new and different programs, ill try to search and learn equals() method @ internet, Thanks for your reply.

Similar Threads

  1. Sin() Wrong Results
    By MemoNick in forum New To Java
    Replies: 1
    Last Post: 12-07-2011, 03:21 PM
  2. wrong results with recursion
    By sara12345 in forum New To Java
    Replies: 0
    Last Post: 04-12-2010, 05:49 PM
  3. Wrong Numeric Results with Right Math Implementation.
    By Übermenschen in forum Advanced Java
    Replies: 4
    Last Post: 04-02-2010, 03:52 AM
  4. InsertionSort My SourceCode - wrong results
    By JohnF8FJohn in forum New To Java
    Replies: 1
    Last Post: 11-27-2008, 02:11 AM
  5. showing results in a for loop randomly
    By vexity in forum New To Java
    Replies: 4
    Last Post: 04-29-2008, 05:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •