HW02 Solution2

HW02 Solution2 - String replyString int correct = number1...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 import javax.swing.JOptionPane public class HW02_2 { public static void main(String args[]) { // 1. Generate two random single-digit integers int number1 = (int)(Math.random() * 10) int number2 = (int)(Math.random() * 10) // 2. If number1 < number2, swap number1 with number2 if (number1 < number2) { int temp = number1 number1 = number2 number2 = temp } String answerString = JOptionPane.showInputDialog ("What is " + number1 + " - " + number2 + "?") int answer = Integer.parseInt(answerString) // 4. Grade the answer and display the result
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: String replyString int correct = number1 - number2 if (correct == answer) replyString = "You are correct!" else if (-1 <= correct - answer && correct - answer <= 1) replyString = "You're really close, but the answer is " + correct else replyString = "Your answer is wrong.\n" + number1 + " - " + number2 + " should be " + (number1 - number2) JOptionPane.showMessageDialog(null, replyString) } } // 3. Prompt the student to answer hwhat is number1 h number2?h...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online