Addition Pop Quiz §
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// addition pop quiz
// caleb b
Scanner input = new Scanner(System.in);
int correctAnswers = 0;
for (int i = 0; i < 5; i++) {
int num1 = (int)(Math.random() * 20);
int num2 = (int)(Math.random() * 20);
int correctAnswer = num1 + num2;
System.out.print("What is " + num1 + " + " + num2 + "? ");
int userAnswer = input.nextInt();
if (userAnswer == correctAnswer) {
System.out.println("You are correct!");
correctAnswers++;
} else {
System.out.println("You are incorrect.");
}
}
System.out.println("You got " + correctAnswers + "/5 correct answers.");
System.out.println("(" + (correctAnswers / 5.0) * 100 + "%)");
}
}
Simulate Rolling a Pair of Dice §
public class Main {
public static void main(String[] args) {
// dice roll simulation
// caleb b
int roll1 = (int) (Math.random() * 6) + 1;
int roll2 = (int) (Math.random() * 6) + 1;
int sum = roll1 + roll2;
System.out.println("Roll 1: " + getRoll(roll1));
System.out.println("Roll 2: " + getRoll(roll2));
System.out.println("Sum: " + getRoll(sum));
}
public static String getRoll(int roll) {
switch (roll) {
case 1:
return "ONE";
case 2:
return "TWO";
case 3:
return "THREE";
case 4:
return "FOUR";
case 5:
return "FIVE";
case 6:
return "SIX";
case 7:
return "SEVEN";
case 8:
return "EIGHT";
case 9:
return "NINE";
case 10:
return "TEN";
case 11:
return "ELEVEN";
case 12:
return "TWELVE";
default:
return "INVALID";
}
}
}
View Next Assignment