Ok, so, onto another problem. I have another simple program that will allow people to put in a number, then put in another, then answer some math questions about it. When they finish answering, it displays a message for them, tells them how many they got correct, and tells them their percentage correct.
My main issues are:
1. When it tells them how many they got correct, it gives 1.0, 2.0, 3.0 etc. instead of 1, 2, 3. I know this has to do with int and double, but I can't figure out how to make them convert neatly (or at all).
2. When it gives them a percentage, I need it to round to five decimal places. I'm not sure how to do this at all.
Anybody know what's up?
Here is my code:
import javax.swing.JOptionPane;
public class Lab3Part2 {
public static void main(String[] args) {
String input;
int firstInt, secInt, productInt, quotientInt, remainderInt, answerInt1, answerInt2, answerInt3;
double score1, score2, score3, totalScore, totalAvg, percentage;
input = JOptionPane.showInputDialog("Please enter an integer.");
firstInt = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Please enter another integer.");
secInt = Integer.parseInt(input);
productInt = firstInt * secInt;
quotientInt = firstInt / secInt;
remainderInt = firstInt % secInt;
JOptionPane.showMessageDialog(null, "Answer the following questions: ");
input = JOptionPane.showInputDialog("1. " + firstInt + " * " + secInt + " = ?");
answerInt1 = Integer.parseInt(input);
if (answerInt1 == productInt) {
JOptionPane.showMessageDialog(null, "Correct.");
score1 = 1;
}
else {
JOptionPane.showMessageDialog(null, "Wrong!");
score1 = 0;
}
input = JOptionPane.showInputDialog("2. " + firstInt + " / " + secInt + " = ?");
answerInt2 = Integer.parseInt(input);
if (answerInt2 == quotientInt) {
JOptionPane.showMessageDialog(null, "Correct.");
score2 = 1;
}
else {
JOptionPane.showMessageDialog(null, "Wrong!");
score2 = 0;
}
input = JOptionPane.showInputDialog("3. " + firstInt + " % " + secInt + " = ?");
answerInt3 = Integer.parseInt(input);
if (answerInt3 == remainderInt) {
JOptionPane.showMessageDialog(null, "Correct.");
score3 = 1;
}
else {
JOptionPane.showMessageDialog(null, "Wrong!");
score3 = 0;
}
totalAvg = (score1 + score2 + score3) / 3;
totalScore = (score1 + score2 + score3);
if (totalAvg == 0) {
JOptionPane.showMessageDialog(null, "You did very bad.");
}
else if (totalAvg > .33 && totalAvg < .66) {
JOptionPane.showMessageDialog(null, "You did poorly.");
}
else if (totalAvg >= .66 && totalAvg < 1) {
JOptionPane.showMessageDialog(null, "You did OK.");
}
else if (totalAvg == 1) {
JOptionPane.showMessageDialog(null, "Good Job!");
}
JOptionPane.showMessageDialog(null, "You got " + totalScore + " correct.");
percentage = totalAvg * 100;
JOptionPane.showMessageDialog(null, "That's " + percentage + "%");
System.exit(0);
}
}