• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

math program help..

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this project I need help with.. I am supposed to creat a simple math quiz/test and it will ask five questions each with four multiple choice answers..

The program needs to tell whether they are correct or incorrect for each answer, as well as display the correct answer if wrong.. It will also need to calculate the total percentage of correct answers at the end of the program..

I am listing what I have made so far, and wanted to know if something looks wrong here..



Any help is greatly appreciated...
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


wanted to know if something looks wrong here



Yes, there are a few issues here.

I think you'll find many people willing to help, but unwilling to give you the solution.

I suggest asking a specific question. You'll get a specific answer that will get you closer to your goal.

It may be easier if you write your logic as psuedocode. For example:



That might allow you to focus on the logical steps without worrying about Java syntax. Once you've got the psuedocode correct, you can fill in around the comments with real code.
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to compile and am getting 'else' without 'if' errors..
Here's the new code I am trying to compile..



I haven't started on the percentage calculation, or know how I should go about it regarding how many answers are right.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
might be easier for you if you were to create a Question object.
possible fields - the question itself, an array of choices, the correct answer etc
possible methods - displayQuestion(), displayChoices() etc

you can now put these Questions into a Question[]

iterate the Question[]:
display the question
display the choices
read the answer
compare to Question object's correct answer (if correct, correctCounter++)

do your percentages
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think I can do the Question object thing because I haven't learned it yet and I think they want me to use only what I have learned so far..

Is that like a switch statement?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In Java, the "if" and "else" statements only execute one line (that follows it). If you want to run more than one line, you need to put it in blocks -- or you'll get "else without if" compilation errors.

Basically like this...



Henry
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Is that like a switch statement?

No, it's a class



then you create the objects
Question q1 = new Question(...);
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, now I'm getting variable a3,a4, and a5 might not have been initialized..
I thought I did initialize them correctly because the first two work ok..

[ September 03, 2006: Message edited by: Jeremy Parsons ]
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeremy Parsons:
I don't think I can do the Question object thing because I haven't learned it yet and I think they want me to use only what I have learned so far..



That would be the day, a teacher who's unhappy with his students learning on their own...
Of course most teachers these days (and rightly so given the attitude of most students these days) don't expect their pupils to put in any effort beyond the bare minimum to get a passing grade (and preferably turn in work done by others instead), but that doesn't mean they wouldn't like things to be different.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeremy Parsons,

The following is the code and tested and it works fine, here you need to understand that after reading the input(you should not support to read 3 times as you done in your program) you should clean the buffer for that I am using 2 dummy read statements(any one is there any better way?).

import java.io.IOException;

public class MathTest
{

public static void main(String[]args) throws Exception
{

int a1,a2,a3,a4,a5;
int userInput;


// Question #1
System.out.println("1. 7 + 2 = ? a.6 b.8 c.9 d.10");
System.out.print("Enter your answer: ");
a1 = System.in.read();
userInput=System.in.read();
userInput=System.in.read();

if (a1 == 'c')
{

System.out.println("Correct!");
System.out.println();
}
else
{
System.out.println("Sorry! The correct answer is c");
System.out.println();
}

//Question #2
System.out.println("2. 9 - 5 = ? a.2 b.4 c.6 d.8");
System.out.print("Enter your answer: ");
a2 = System.in.read();
userInput=System.in.read();
userInput=System.in.read();

if (a2 == 'b')
{
System.out.println("Correct!");
System.out.println();
}
else
{System.out.println("Sorry! The correct answer is b");
System.out.println();
}
//Question #3
System.out.println("3. 144/12 = ? a.10 b.12 c.14 d.16");
System.out.print("Enter your answer: ");
a3 = System.in.read();
userInput=System.in.read();
userInput=System.in.read();

if (a3 == 'b')
{
System.out.println("Correct!");
System.out.println();
}
else
{System.out.println("Sorry! The correct answer is b");
System.out.println();
}
//Question #4
System.out.println("4. 5 * 5 = ? a.10 b.20 c.25 d.125");
System.out.print("Enter your answer: ");
a4 = System.in.read();
userInput=System.in.read();
userInput=System.in.read();

if (a4 == 'c')
{
System.out.println("Correct!");
System.out.println();
}
else
{System.out.println("Sorry! The correct answer is c");
System.out.println();
}
//Question #5
System.out.println("5. 2 + 3 + 5 = ? a.7 b.8 c.9 d.10");
System.out.print("Enter your answer: ");
a5 = System.in.read();
userInput=System.in.read();
userInput=System.in.read();

if (a5 == 'd')
{
System.out.println("Correct!");
System.out.println();
}
else
{System.out.println("Sorry! The correct answer is d");
System.out.println();
}




}
}

-Ram.
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, first of all I am not in a "class", I am self teaching through a distance learning school..

Second, I'm not asking someone to do my program for me and I turn it in, etc.. I am seeking help, but you are not helping, instead acting like an ass...

I'm working to figure out what my problem with my code is, and that's what this website is all about is Java, learning, and helpful people..

If you don't like it, stay off my posts...
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ok, now I'm getting variable a3,a4, and a5 might not have been initialized..
I thought I did initialize them correctly because the first two work ok..



Take a look at your code for question 3, 4, and 5. I believe that you may have gotten a bit aggressive with the cut-n-paste. Did you actually set the a3, a4, and a5 variables?

Henry
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know why I got the last error.. in the line of code

a2 = System.in.read();System.in.read();System.in.read();

After the second one, I accidentally made them all a2 variables beside the input.. I changed and it now compiles..
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol thanks.. I believe we both noticed it at the same time..

Now I just need help figuring out how to calculate correct answers for a total percentage of correct to display..

should I do something like this..

if (a1 && a2 && a3 && a4 && a5)
System.out.println("You graded 100%");

and just repeat that but take one away from each??
 
parshuram bingi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not fisable to do so...., how can you do the same if you are having more no of question. I suggest to go with the counter. when ever you find a correct answer increment the counter and finally you can computer the percentage with the counter.

-ram
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ah, didn't think about a counter, I was thinking way too complex or something.. Thanks, I'll try a counter and post the results..
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have completed the code, and the counter works, but when my calculation into a percentage displays say if I get all 5 right, it only displays 20%

Here's the full code..


What do I need to look at to fix the calculation? PS I'm not that great at math as it is...lol
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok I got it, you can disregard the last post.. I should have taken down the division part..
 
I child proofed my house but they still get in. Distract them with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic