Welcome to Dream.In.Code
Become a Java Expert!

Join 150,422 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,100 people online right now. Registration is fast and FREE... Join Now!




Multiplication Drill and Grade Help......beginner

 
Reply to this topicStart new topic

Multiplication Drill and Grade Help......beginner, I could really use another set of eyes, and some assistance....

tami3113
17 Mar, 2008 - 05:48 PM
Post #1

New D.I.C Head
*

Joined: 3 Nov, 2007
Posts: 17


My Contributions
I have an assignment for my beginners java course (which is an online course). I have to drill a student on 20 multiplication tables (random numbers 1-20) and then once all 20 questions are answered, I must give a grade.
I've been using my text (Intro to Java Programming), and looked through various sites on the web to try to better explain different ways to accomplish this. So far this is what I come up with. This code has been written from using the lessons in my text. The code compiles, but I get a number of errors. My biggest problem is that it does not cycle through 20 problems. It only goes through one. Thus the rest of the program doesnt execute, and I obviously cant get a grade. Some assistance and even explinations would be helpful! Thanks smile.gif

import javax.swing.JOptionPane;

public class multDrill {
public static void main(String[] args) {

int count = 0; //count number problems
int score = 0; //begin count number right

while (count < 21){
//generate two random numbers
int number1 = (int)(Math.random(1) * 20);
int number2 = (int)(Math.random(1) * 20);
}
//give mult problem to student
String answerString = JOptionPane.showInputDialog
("What is" + number1 + " * " + number2 + " ? ");
int answer = Integer.parseInt(answerString);
//correct or incorrect
String replyString;
if (number1 * number2 == answer){
replyString = "Good Job";
score++;
}
else
replyString = "Incorrect";
JOptionPane.showMessageDialog(null, replyString);

//increase count by 1
count++;
}

{
if (score >= 18)
grade = 'A';
else if (score >= 16)
grade = 'B';
else if (score >= 14)
grade = 'C';
else if (score >= 12)
grade = 'D';
else
grade = 'F';
}
JOptionPane.showMessageDialog(null,
"Your grade is" + grade + );

}
}

User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Multiplication Drill And Grade Help......beginner
17 Mar, 2008 - 06:04 PM
Post #2

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
First, If you post your code in the code tags, it makes it more readable and easier to help you find the errors.

Here is the code, the changes have been made and it works as you might want to have it:

CODE

package solvers3;

import javax.swing.JOptionPane;

public class multdrill {
    public static void main(String[] args) {
    
    int count = 0; //count number problems
    int score = 0; //begin count number right
    
    
    char grade = 'z'; //declaring grade
    
    
    
    while (count < 21) {
        //generate two random numbers
        int number1 = (int)(Math.random() * 20); //random() method does not take any parameters
        int number2 = (int)(Math.random() * 20);
        
    //YOUR WHILE LOOP terminated here
        
        //give mult problem to student
        String answerString = JOptionPane.showInputDialog
        ("What is " + number1 + " * " + number2 + " ? ");
        
        int answer = Integer.parseInt(answerString);
        //correct or incorrect
        
        String replyString;
        if (number1 * number2 == answer){
            replyString = "Good Job";
            score++;
        }
        else
        replyString = "Incorrect";
        JOptionPane.showMessageDialog(null, replyString);
        
        //increase count by 1
        count++;
    
    }//end of while loop
    
    if (score >= 18)
        grade = 'A';       //grade was not defined
    else if (score >= 16)
        grade = 'B';
    else if (score >= 14)
        grade = 'C';
    else if (score >= 12)
        grade = 'D';
    else
        grade = 'F';
    
    JOptionPane.showMessageDialog(null,
    "Your grade is " + grade ); //changed
    
    }
}



I have commented the code, try to understand and ask if you have any doubts. Happy Coding! :)
User is offlineProfile CardPM
+Quote Post

Steven Smith
RE: Multiplication Drill And Grade Help......beginner
17 Mar, 2008 - 06:09 PM
Post #3

New D.I.C Head
*

Joined: 17 Mar, 2008
Posts: 25


My Contributions
EDIT: Beaten by 5 minutes confused.gif

Few things I changed...

Initialized grade.
Removed bracket closing while loop after defining your two variables.
Changed some spacing.

Compiled and ran perfectly after that.


CODE


package test;

/**
*
* @author Steven Smith
*/
import javax.swing.JOptionPane;

public class Main{
    
public static void main(String[] args) {

int count = 0; //count number problems
int score = 0; //begin count number right
char grade ='N';

while (count < 21){
//generate two random numbers
int number1 = (int)(Math.random() * 20);
int number2 = (int)(Math.random() * 20);

//give mult problem to student
String answerString = JOptionPane.showInputDialog("What is " + number1 + " * " + number2 + " ? ");
int answer = Integer.parseInt(answerString);
//correct or incorrect
String replyString;
if (number1 * number2 == answer){
replyString = "Good Job";
score++;
}
else
replyString = "Incorrect";
JOptionPane.showMessageDialog(null, replyString);

//increase count by 1
count++;
}

if (score >= 18)
    grade = 'A';
else if (score >= 16)
    grade = 'B';
else if (score >= 14)
    grade = 'C';
else if (score >= 12)
    grade = 'D';
else
    grade = 'F';



JOptionPane.showMessageDialog(null, "The grade is: " + grade);

}
}



This post has been edited by Steven Smith: 17 Mar, 2008 - 06:10 PM
User is offlineProfile CardPM
+Quote Post

tami3113
RE: Multiplication Drill And Grade Help......beginner
17 Mar, 2008 - 06:23 PM
Post #4

New D.I.C Head
*

Joined: 3 Nov, 2007
Posts: 17


My Contributions
Both seem to work very well. Thank you very much to both of you. I'm going to play around with them just to get more familiar with the way that Java works. While online classes are so convenient, sometimes you lose out on that one on one instruction that is so very valuable.

Thank you again!!!!!! biggrin.gif icon_up.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:25PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month