Question: I need help correcting this java program. I can't seem to find the error in this code which has been provided for this riddle: A

I need help correcting this java program. I can't seem to find the error in this code which has been provided for this riddle:

A high school has 1000 students and 1000 lockers, one locker for each student. On the first day of school, the principal plays the following game: She asks the first student to open all the lockers. She then asks the second student to close all the even-numbered lockers. The third student is asked to check every third locker. If it is open, the student closes it; if it is closed, the student opens it. The fourth student is asked to check every fourth locker. If it is open, the student closes it; if it is closed, the student opens it. The remaining students continue this game. In general, the nth student checks every nth locker. If it is open, the student closes it; if it is closed, the student opens it. After all the students have taken turns, some of the lockers are open and some are closed.

import java.util.Scanner;

public class lockers{

public static void main(String[] args){

Scanner keyboard = new Scanner(System.in);

int studentVisitCount = 0;

System.out.print("Enter the number of lockers: ");

int numberOfLockers = console.nextInt();

for(int x=0; x<=numberOfLockers; x++){

if(x%y==0){

studentVisitCount++;

}

for(int y=0; y<=x; y++){

if(studentVisitCount%2!=0){

System.out.print(y+" ");

}

}

}

System.out.println("The number of lockers and students are: "+numberOfLockers);

System.out.print("The locker numbers of lockers that are left open at the end of the game are: ");

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!