Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Question how to mark as Correct or Wrong ?!

    Hey guys ,
    i got question around mark a value as correct or wrong and then save it for further usage ,
    for example , i want to ask user to answer a mathematical question , if the answer is correct , it marks the answer as correct , if not , it'll mark it as wrong . here is my attempt >

    Java Code:
    import java.util.Scanner;
    public class test {
    public static void main (String args []){
    int answer;
    Scanner input=new Scanner(System.in);
    System.out.print(" 5+5 = ? " );
    answer=input.nextInt(); 
    if (answer=10) {// i need to store the answer as correct or wrong // what should i type here ??? //

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    Java Code:
    if (answer=10)
    should be
    Java Code:
    if (answer[COLOR="Magenta"]==[/COLOR]10)

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    In java == is for comparison operator.But in ur code ur using = "assignment operator".Try the below code.

    import java.util.Scanner;
    public class Test
    {
    public static void main (String args [])
    {
    int answer;
    Scanner input=new Scanner(System.in);
    System.out.print(" 5+5 = ? " );
    answer=input.nextInt();
    if (answer==10)
    {// i need to store the answer as correct or wrong // what should
    System.out.println("Correct ");
    }
    else
    {
    System.out.println("Wrong ");
    }//if-else
    }//main
    }

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    for storing ans...
    you can declare a boolean variable, default fase and if answer is correct, you set it to true

  5. #5
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    to Ramya ,
    i dont wanna print out the message ! actually i'm gonna mark the entered answer as correct or wrong and store it , program will ask 10 questions and user answer , then it calculate the answers and show how many are correct and how many are wrong . in order to compare all answers , i need to mark'em all as either

  6. #6
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    to mtyoung ,
    i've tried this and i know it's so foolish !

    import java.util.Scanner;
    public class faq {
    public static void main (String args[]){
    int answer;
    boolean answer;
    Scanner inpur=new Scanner(System.in);
    System.out.print(" 5+5 = ? " );
    answer=input.nextInt();
    if (answer==10) {answer=true;}
    else {answer=false;}

    so what changes should make ?

  7. #7
    kbindumadhavi is offline Member
    Join Date
    Apr 2009
    Posts
    20
    Rep Power
    0

    Default

    Try using an array to store the results either true or false.
    So that you can retrieve the values for calculating number of correct and wrong answers.
    If number of questions are dynamic, then you can use ArrayList for the same.

  8. #8
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    if you want to count number of correct answers, i suggest to use a int variable(may be correctCount) which initialized to 0, and if user answer one correct question, correctCount++

    so, you know the total questions(you may also store it in some int variable), and number of correct answers, then you can found out the number of wrong answers

  9. #9
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    hey guys take a look at this , what's wrong with it ?

    Java Code:
    import java.util.Scanner;
    public class faq {
    public static void main (String args[]){
    Scanner inpur=new Scanner(System.in);
    int answer,correct,count;
    count=0;
    while (answer=9999){
    System.out.print(" 5+5 = ? " );
    answer=input.nextInt(); 
    if (answer==100){
    correct++;
    System.out.print(correct);}
    count++;}
    }
    }

  10. #10
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    any idea friends ?

  11. #11
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Java Code:
    		Scanner input = new Scanner(System.in);
    		boolean correct = false;
    		int answer, count;
    		count=0;
    		while (!correct){
    			System.out.print(" 5+5 = ? " );
    			answer = input.nextInt(); 
    			if (answer == 10){
    				System.out.print("Correct after "+count+" guesses");
    				correct = true;
    			}
    		count++;
    		}
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  12. #12
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    1) need to initialize "correct" to 0

    2) while (answer=9999)
    i dont know why you want to check with answer, but, for comparison, == should be used
    i think the "answer" should be "count"


    3) if (answer==100)
    why check with 100, not 10?

  13. #13
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    this is what i got :

    Java Code:
    import java.util.Scanner;
    public class faq {
    public static void main (String args[]){
    Scanner input = new Scanner(System.in);
    		boolean correct = false;
    		int answer, count;
    		count=0;
    		while (!correct){
    			System.out.print(" 5+5 = ? " );
    			answer = input.nextInt(); 
    			if (answer == 10){
    				System.out.print("Correct after "+count+" guesses");
    				correct = true;
    				
    				System.out.print(" 5+10 = ? " );
    			answer = input.nextInt(); 
    			if (answer == 15){
    				System.out.print("Correct after "+count+" guesses");
    				correct = true;
    			}
    		count++;
    		System.out.print(count++);
    		}
    		}
    		}
    		
    		}
    i want the program display the number of correct n' wrong answer as user finished the quize , i used s.o.p(count++) in order to display correct answers in the end but it doesn't work ...

  14. #14
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    once user get the correct answer, you need to increase count

    so after correct = true, you need to increase count by 1

  15. #15
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Question

    guys this is my latest try on this but still got prob , after finishing the test it'll show # 1 , any idea ?

    Java Code:
    import java.util.Scanner;
    public class faq1 {
    public static void main (String args[]){
    Scanner input = new Scanner(System.in);
    		boolean correct = false;
    		int count=0;
    		int countanswer=0;
    		while (answer!=9999){
    		count++;
    			System.out.print(" 5+5 = ? " );
    			answer = input.nextInt(); 
    			if (answer == 10) {
    				correct = true;
    				countanswer++; }
    				else {System.out.print("not correct");}
    				
    				System.out.print(" 5+5 = ? " );
    			answer = input.nextInt(); 
    			if (answer == 10) {
    				correct = true;
    				countanswer++; }
    				else {System.out.print("not correct");}
    							
    							System.out.print(" 5+5 = ? " );
    			answer = input.nextInt(); 
    			if (answer == 10) {
    				correct = true;
    				countanswer++; }
    				else {System.out.print("not correct");}
    				
    			
    				
    				System.out.print(" 5+5 = ? " );
    			answer = input.nextInt(); 
    			if (answer == 10) {
    				correct = true;
    				countanswer++; }
    				else {System.out.print("not correct");}
    				
    				
    			System.out.print(" 5+5 = ? " );
    			answer = input.nextInt(); 
    			if (answer == 10) {
    				correct = true;
    				countanswer++; }
    				else {System.out.print("not correct");}
    				
    				
    				System.out.print(" 5+5 = ? " );
    			answer = input.nextInt(); 
    			if (answer == 10) {
    				correct = true;
    				countanswer++; }
    				else {System.out.print("not correct");}
    				
    				System.out.print(" 5+5 = ? " );
    			answer = input.nextInt(); 
    			if (answer == 10) {
    				correct = true;
    				countanswer++; }
    				else {System.out.print("not correct");}
    				
    				
    				System.out.print(" 5+5 = ? " );
    			answer = input.nextInt(); 
    			if (answer == 10) {
    				correct = true;
    				countanswer++; }
    				else {System.out.print("not correct");}
    				
    				
    				System.out.print(" 5+5 = ? " );
    			answer = input.nextInt(); 
    			if (answer == 10) {
    				correct = true;
    				countanswer++; }
    				else {System.out.print("not correct");}
    				
    			System.out.print(" 5+5 = ? " );
    			answer = input.nextInt(); 
    			if (answer == 10) {
    				correct = true;
    				countanswer++; }
    				else {System.out.print("not correct");}
    				
    		countanswer = (countanswer/count*100);
    		System.out.print(countanswer);
    		}
    		}
    		}

  16. #16
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    what is "answer" in the while statement?
    boolean correct use for?

    in while statement, why set to check not equal to 9999?
    so how answer can be 9999 to stop the loop?

  17. #17
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    I don't really know if I understand correctly but initially:
    1. You have 10 questions
    2. You want to mark if incorrect or correct every time a question is answered.


    So why not simply have 2 parallel arrays (Means that you have an array of integers and an array of boolean)

    you loop from 0 to 9, every time inside a loop check for the correct answer, if answer is true you assign a boolean to the corresponding answer to true.

    I am looking at your code now and I don't really understand why you are writing 10 logical blocks of code inside a while loop.
    Tshegofatso Manakana
    a.k.a Untouchable

  18. #18
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    to Tshegofatsom , i'm sorry but i dont get your point ! could gimme an example ?

  19. #19
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    I didn't compile the code but here is the logic. You can even take it further by randomizing the questions since this
    Java Code:
    Scanner input = new Scanner(System.in);
            int numquestions = 10;
            int answer[] = new int[numquestions]; //
            boolean correct[] = new boolean[numquestions];
            //The above are parallel arrays(have same number of elements)
            //Means that the question[0] will correspond to correc[0] and so on
            //10 can be repplaced according to number of questions you want (user can enter it)
            
            int numberOfcorrectAnswers = 0; //keeps track of correct answerrs
            
            //N.B By the end of this look you still have your values stored in the two arrays
            for(int x = 0; x < 10; x++){
                
                System.out.print("Ask question (5 + 5 = ?): ");//good idea to randomise it. Avoid hardcoding
                answer[x] = input.nextInt(); 
                if(answer[x] == 10){ //validate answer. There can be a better way to do this. Avoid hard coding
                    correct[x] = true;
                    numberOfcorrectAnswers++;                
                }
                //Disply the number of correct answers here or the average
                //Remember you still have your values here, you can still loop through them
            }
    Tshegofatso Manakana
    a.k.a Untouchable

  20. #20
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    thanks , but , what if i want to ask 10 different questions? still using for loop?

Page 1 of 2 12 LastLast

Similar Threads

  1. Help needed for a 40 mark homework due in 1 hour .. plz
    By q8ysurgeon in forum New To Java
    Replies: 5
    Last Post: 01-09-2009, 05:07 PM
  2. Help needed for a 40 mark homework due in 1 hour .. plz
    By q8ysurgeon in forum Advanced Java
    Replies: 3
    Last Post: 01-09-2009, 04:09 AM
  3. Mark thread RESOLVED.
    By Eranga in forum Suggestions & Feedback
    Replies: 45
    Last Post: 04-02-2008, 10:34 AM
  4. BufferedReader .mark(int readAheadLimit)
    By ShoeNinja in forum New To Java
    Replies: 1
    Last Post: 11-16-2007, 10:58 PM

Posting Permissions

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