Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions