This code won't run due to an error in the last line. It needs to prompt the user for three student names using joptionpane. Then randomly assign house colours to each student such as red, white or blue. It also needs to assign student numbers to each student. The output of the code is supposed to be a joptionpane message that reads:
SCHOOL HOUSE ASSIGNMENT
MARK assigned to the blue house with student number BLUE870
PETER assigned to the white house with student number WHITE41
Jabulani assigned to the red house with student number RED935
Below is my attempt:
import javax.swing.JOptionPane;
import java.util.*;
public class NewClass {
public static void main(String[] args) {
String student1;
String student2;
String student3;
student1 = JOptionPane.showInputDialog(null, "Please enter first student name");
student2 = JOptionPane.showInputDialog(null, "Please enter second student name");
student3 = JOptionPane.showInputDialog(null, "Please enter third student name");
Random rand = new Random();
int rand_int1 = rand.nextInt(10);
int rand_int2;
int rand_int3;
Scanner input = new Scanner(System.in);
String[] name = new String[3];
int[] house = new int[3];
int[] flag = new int[3];
String[] house_name = new String[] {
"red",
"blue",
"white"
};
house[0] = (rand_int1 % 3) + 1;
flag[rand_int1 % 3] = 1;
while (house[1] == 0 || house[2] == 0) {
rand_int2 = rand.nextInt(10);
if (flag[rand_int2 % 3] == 0) {
house[1] = (rand_int2 % 3) + 1;
flag[rand_int2 % 3] = 1;
}
rand_int3 = rand.nextInt(10);
if (flag[rand_int3 % 3] == 0) {
house[2] = (rand_int3 % 3) + 1;
flag[rand_int3 % 3] = 1;
}
}
int rand_int = rand.nextInt((9999 - 100) + 1) + 10;
rand_int = rand.nextInt((9999 - 100) + 1) + 10;
rand_int = rand.nextInt((9999 - 100) + 1) + 10;
JOptionPane.showMessageDialog(null, "SCHOOL HOUSE ASSIGNMENT" + "\n******************************************" + \nstudent1 + "assigned to the" + house_name[house[0] - 1] + "with student number" + house[0] + rand_int1 + \nstudent2 + "assigned to the" + house_name[house[1] - 1] + "with student number" + house[1] + rand_int2 + \nstudent3 + "assigned to the" + house_name[house[2] - 1] + "with the student number" + house[2] rand_int3);
}
}
Aucun commentaire:
Enregistrer un commentaire