Results 1 to 20 of 21
- 05-04-2009, 06:15 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
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 ??? //
- 05-04-2009, 06:44 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
should beJava Code:if (answer=10)
Java Code:if (answer[COLOR="Magenta"]==[/COLOR]10)
- 05-04-2009, 06:49 AM #3
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
}
- 05-04-2009, 07:06 AM #4
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
for storing ans...
you can declare a boolean variable, default fase and if answer is correct, you set it to true
- 05-04-2009, 09:12 AM #5
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
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
- 05-04-2009, 09:13 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
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 ?
- 05-04-2009, 09:45 AM #7
Member
- Join Date
- Apr 2009
- Posts
- 20
- Rep Power
- 0
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.
- 05-04-2009, 10:20 AM #8
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
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
- 05-05-2009, 02:50 PM #9
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
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++;} } }
- 05-05-2009, 06:30 PM #10
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
any idea friends ?
- 05-05-2009, 06:47 PM #11
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.
- 05-06-2009, 07:44 AM #12
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
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?
- 05-06-2009, 07:53 AM #13
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
this is what i got :
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 ...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++); } } } }
- 05-06-2009, 08:00 AM #14
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
once user get the correct answer, you need to increase count
so after correct = true, you need to increase count by 1
- 05-20-2009, 02:11 PM #15
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
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); } } }
- 05-21-2009, 02:11 AM #16
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
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?
- 05-21-2009, 09:29 AM #17
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
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 ™
- 05-21-2009, 09:45 AM #18
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
to Tshegofatsom , i'm sorry but i dont get your point ! could gimme an example ?
- 05-21-2009, 10:09 AM #19
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
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 ™
- 05-21-2009, 10:24 AM #20
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
Similar Threads
-
Help needed for a 40 mark homework due in 1 hour .. plz
By q8ysurgeon in forum New To JavaReplies: 5Last Post: 01-09-2009, 05:07 PM -
Help needed for a 40 mark homework due in 1 hour .. plz
By q8ysurgeon in forum Advanced JavaReplies: 3Last Post: 01-09-2009, 04:09 AM -
Mark thread RESOLVED.
By Eranga in forum Suggestions & FeedbackReplies: 45Last Post: 04-02-2008, 10:34 AM -
BufferedReader .mark(int readAheadLimit)
By ShoeNinja in forum New To JavaReplies: 1Last Post: 11-16-2007, 10:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks