Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default need some advices!

    I thought i had this right but no...

    need some help to point out where are my mistakes:)

    Java Code:
    import java.util.Scanner;
    
    	public class HW{
    
    	public static void main(String[]args){
    
    		System.out.println("Math Quiz");
    
    		Scanner input = new Scanner(System.in);
    
    		System.out.println("Q1: 12+3=?");
    
    		int a = input.nextInt();
    
    		if (a == 15){
    		int Q1 = 1;
    }
    		else {
    		int Q1 = 0;
    }
    		System.out.println("Q2: 2+4=?");
    
    		int b = input.nextInt();
    
    		if (b == 6){
    			int Q2 = 1;
    }
    		else {
    			int Q2 = 0;
    }
    		System.out.println("Q3: 5+6=?");
    
    		int c = input.nextInt();
    
    		if (c == 6){
    			int Q3 = 1;
    }
    		else {
    			int Q3 = 0;
    }
    		System.out.println("Q4: 7+10=?");
    
    		int d = input.nextInt();
    
    		if (d == 17){
    			int Q4 = 1;
    }
    		else {
    			int Q4 = 0;
    }
    		System.out.println("Q5: 11+20=?");
    
    		int e = input.nextInt();
    
    		if (e == 6){
    			int Q5 = 1;
    }
    		else {
    			int Q5 = 0;
    
    }
    		int total = Q1 + Q2 + Q3 + Q4 + Q5;
    
    		if (total>=4){
    			System.out.print("You got"+total+" correct. Your grade is A.");
    }
    		else if (total>=3){
    			System.out.print("You got"+total+" correct. Your grade is B.");
    }
    		else if  (total>=2){
    		System.out.print("You got"+total+" correct. Your grade is C.");
    }
    		else {
    		System.out.print("You got"+total+" correct. Your grade is NC.");
    }
    
    	}
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    int total = Q1 + Q2 + Q3 + Q4 + Q5;
    i think its a syntax error, but not sure how to fix it.

  4. #4
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    You can't say int Q1 inside a if/else loop if you plan using Q1 in other places. Same goes for Q2/Q3/Q4/Q5 Try doing

    Java Code:
    int Q1 = 0;
    
    if (a == 15)
    {
      Q1 = 1;
    }
    else
    {
      Q1 = 0;
    }
    Edit: Check c and e in the if statements, those values are wrong.
    Last edited by Bomber_Will; 01-17-2009 at 10:05 AM. Reason: Need Code Tags

  5. #5
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    nop it doesnt work.:(

  6. #6
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    Post your new code here, I got it to work by doing that change on all int Q's

  7. #7
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    Upgrade suggestions:

    * try using the random method in Math class to generate the numbers.

    * try using a single variable to add up the scores.

  8. #8
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    this code is after putting Qs outside of the loop
    Java Code:
    import java.util.Scanner;
    
    	public class HW1{
    
    	public static void main(String[]args){
    
    		System.out.println("Math Quiz");
    
    		Scanner input = new Scanner(System.in);
    
    		System.out.println("Q1: 12+3=?");
    
    		int a = input.nextInt();
    		int Q1 = 0;
    
    		if (a == 15){
    		int Q1 = 1;
    }
    		else {
    		int Q1 = 0;
    }
    		System.out.println("Q2: 2+4=?");
    
    		int b = input.nextInt();
    		int Q2 = 0;
    		if (b == 6){
    		int Q2 = 1;
    }
    		else {
    			int Q2 = 0;
    }
    		System.out.println("Q3: 5+6=?");
    
    		int c = input.nextInt();
    		int Q3 = 0;
    		if (c == 11){
    			int Q3 = 1;
    }
    		else {
    			int Q3 = 0;
    }
    		System.out.println("Q4: 7+10=?");
    
    		int d = input.nextInt();
    		int Q4 = 0;
    		if (d == 17){
    			int Q4 = 1;
    }
    		else {
    			int Q4 = 0;
    }
    		System.out.println("Q5: 11+20=?");
    
    		int e = input.nextInt();
    		int Q5 = 0;
    		if (e == 31){
    			int Q5 = 1;
    }
    		else {
    			int Q5 = 0;
    
    }
    		int total = Q1 + Q2 + Q3 + Q4 + Q5;
    
    		if (total>=4){
    			System.out.print("You got"+total+" correct. Your grade is A.");
    }
    		else if (total>=3){
    			System.out.print("You got"+total+" correct. Your grade is B.");
    }
    		else if  (total>=2){
    		System.out.print("You got"+total+" correct. Your grade is C.");
    }
    		else {
    		System.out.print("You got"+total+" correct. Your grade is NC.");
    }
    
    	}
    }

  9. #9
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    You only need to say int Q2 once. I see you doing

    Java Code:
    int Q2 = 0;
    
    if(b == 6)
    int Q2 = 1;
    else
    int Q2 = 0;
    Get rid of the int inside the if/else statements completely so it looks like

    Java Code:
    int Q2 = 0;
    
    if(b == 6)
    Q2 = 1;
    else
    Q2 = 0;
    Do this for Q1/2/3/4/5

  10. #10
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

Posting Permissions

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