Hi everyone. First time poster, brand new to java but I like it. I'm comfortable with the very basics but as we move forward I'm starting to struggle. My prof is not very good about explaining things so I've been teaching it to myself mostly. My most recent assignment involves creating a program that has the user input two integers and then users those integers to create 3 math problems. The end is supposed to tally all the correct answers and output the user's score and a message. I've got everything to work down to where you tally the answers. I'm not how to do that. I will leave my latest attempt to create the output at the end of the paste. If you could be any help, or at least point me to the correct method to use that would be great. Thanks! -J

import java.util.Scanner;


public class Lab3Part2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int integer1, integer2, answer1, answer2, answer3;
		
		System.out.println("Please enter an integer.");
		Scanner keyboard = new Scanner(System.in);
		integer1 = keyboard.nextInt();
		System.out.println("Please enter another integer.");
		integer2 = keyboard.nextInt();
		System.out.println("");
		System.out.println("Answer the following questions:");
		System.out.println("");
		System.out.println("1. " + integer1 + " * " + integer2 + " = ?");
		answer1 = keyboard.nextInt();
		if (answer1 == integer1 * integer2) 
		
				{System.out.println("Correct.");
				
				}
		
		else
				{System.out.println("Wrong!");
				}
		System.out.println("");
		System.out.println("2. " + integer1 + " / " + integer2 + " = ?");
		answer2 = keyboard.nextInt();
		if (answer2 == integer1 / integer2)
		
				{System.out.println("Correct.");
				
				}
		
	
		else
			
				{System.out.println("Wrong!");
				}
		int a = 0;
		System.out.println("");
		System.out.println("3. " + integer1 + " % " + integer2 + " = ?");
		answer3 = keyboard.nextInt();
		if (answer3 == integer1 % integer2)
			
				{System.out.println("Correct.");
				}	
		else
				{System.out.println("Wrong!");
				}
		
		System.out.println("");
	if (answer1 == integer1 * integer2) 
	{int total = 1;
	
	}
	
	else {int total = 0;
	}
	if (answer2 == integer1 / integer2)
	{int total1 = total + 1;
	
	}
	else
	{int total1 = total;
	}
	
	if (answer3 == integer1 % integer2)
	{int total2 = total1 +1;
	else 
	{int total2 = total1;
	
	}
	}
	}
		{System.out.println("You got " + total2 + "correct.");
		}
		
}

I also have to compute their score, so if they missed 1 of 3, I give them 66.67% I'm just not sure how to hold the values of the score.

Recommended Answers

All 3 Replies

Hi everyone. First time poster, brand new to java but I like it. I'm comfortable with the very basics but as we move forward I'm starting to struggle. My prof is not very good about explaining things so I've been teaching it to myself mostly. My most recent assignment involves creating a program that has the user input two integers and then users those integers to create 3 math problems. The end is supposed to tally all the correct answers and output the user's score and a message. I've got everything to work down to where you tally the answers. I'm not how to do that. I will leave my latest attempt to create the output at the end of the paste. If you could be any help, or at least point me to the correct method to use that would be great. Thanks! -J

import java.util.Scanner;


public class Lab3Part2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int integer1, integer2, answer1, answer2, answer3;
		
		System.out.println("Please enter an integer.");
		Scanner keyboard = new Scanner(System.in);
		integer1 = keyboard.nextInt();
		System.out.println("Please enter another integer.");
		integer2 = keyboard.nextInt();
		System.out.println("");
		System.out.println("Answer the following questions:");
		System.out.println("");
		System.out.println("1. " + integer1 + " * " + integer2 + " = ?");
		answer1 = keyboard.nextInt();
		if (answer1 == integer1 * integer2) 
		
				{System.out.println("Correct.");
				
				}
		
		else
				{System.out.println("Wrong!");
				}
		System.out.println("");
		System.out.println("2. " + integer1 + " / " + integer2 + " = ?");
		answer2 = keyboard.nextInt();
		if (answer2 == integer1 / integer2)
		
				{System.out.println("Correct.");
				
				}
		
	
		else
			
				{System.out.println("Wrong!");
				}
		int a = 0;
		System.out.println("");
		System.out.println("3. " + integer1 + " % " + integer2 + " = ?");
		answer3 = keyboard.nextInt();
		if (answer3 == integer1 % integer2)
			
				{System.out.println("Correct.");
				}	
		else
				{System.out.println("Wrong!");
				}
		
		System.out.println("");
	if (answer1 == integer1 * integer2) 
	{int total = 1;
	
	}
	
	else {int total = 0;
	}
	if (answer2 == integer1 / integer2)
	{int total1 = total + 1;
	
	}
	else
	{int total1 = total;
	}
	
	if (answer3 == integer1 % integer2)
	{int total2 = total1 +1;
	else 
	{int total2 = total1;
	
	}
	}
	}
		{System.out.println("You got " + total2 + "correct.");
		}
		
}

I also have to compute their score, so if they missed 1 of 3, I give them 66.67% I'm just not sure how to hold the values of the score.

well the biggest problem is that most of your totals are out of scope of one another ... so rather declare total,total1 and total2 at the top of your code.
like where you declared integer1 and integer2 etc once you have done that you will need to remove the 'int' identifier in all the totals after it i.e:

if (answer1 == integer1 * integer2) {
            total = 1;//see no more int total=...

        }

another mistake is here:

if (answer3 == integer1 % integer2) {
             int total2 = total1 + 1;
        else {
            int total2 = total1;

        }

you are missing a closing brace before your else statement like this:

if (answer3 == integer1 % integer2) {
             int total2 = total1 + 1;
        } else {
            int total2 = total1;

        }

your other mistake is here:

if (answer3 == integer1 % integer2) {
              int total2 = total1 + 1;
        }else {
             int total2 = total1;

        }
    }
}
{System.out.println("You got " + total2 + "correct.");
		}

your println statement isnt even within the main method it should be like this:

if (answer3 == integer1 % integer2) {
           int total2 = total1 + 1;
        } else {
           int total2 = total1;

        }
        System.out.println("You got " + total2 + "correct.");

with ONLY two closing braces '}' after the println method.

well the biggest problem is that most of your totals are out of scope of one another ... so rather declare total,total1 and total2 at the top of your code.
like where you declared integer1 and integer2 etc once you have done that you will need to remove the 'int' identifier in all the totals after it i.e:

if (answer1 == integer1 * integer2) {
            total = 1;//see no more int total=...

        }

another mistake is here:

if (answer3 == integer1 % integer2) {
             int total2 = total1 + 1;
        else {
            int total2 = total1;

        }

you are missing a closing brace before your else statement like this:

if (answer3 == integer1 % integer2) {
             int total2 = total1 + 1;
        } else {
            int total2 = total1;

        }

your other mistake is here:

if (answer3 == integer1 % integer2) {
              int total2 = total1 + 1;
        }else {
             int total2 = total1;

        }
    }
}
{System.out.println("You got " + total2 + "correct.");
		}

your println statement isnt even within the main method it should be like this:

if (answer3 == integer1 % integer2) {
           int total2 = total1 + 1;
        } else {
           int total2 = total1;

        }
        System.out.println("You got " + total2 + "correct.");

with ONLY two closing braces '}' after the println method.

Thank you so much for the timely response! I'm at work right now but I will definitely work out those errors. Is there a way to have just one integer there at the end that continues to tally? Or do I need to establish multiple integers to keep a running total?

Thank you so much for the timely response! I'm at work right now but I will definitely work out those errors. Is there a way to have just one integer there at the end that continues to tally? Or do I need to establish multiple integers to keep a running total?

No you can use just one intger and then keep adding to that integer:

int total=0;
total=total+1;
total=total+2;
System.out.println(total);//should be 3
//however to add some optimization
int ttal=0;
ttal+=1;
ttal+=2
System.out.println(ttal);//should be 3
//the above does the same as the other code just shortened version
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.