猿问

更衣室问题推广到 n 个人 - 重复错误

我有以下代码:


import java.util.Scanner;

import java.util.Arrays;

public class Program11 {


  public static void main(String args[]) {

    Scanner input = new Scanner(System.in);

    System.out.print("Number of lockers:");

    int number = input.nextInt(); 

    System.out.print("Show stages [y/n]?");

    String show = input.next();

    if(show.equals("y")) 

    {

      for (char[] lockers : getStages(number)) 

      { 

        for (char c : lockers) 

        { 

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

        } 

        System.out.println();

      }

    }

  }


  public static char[][] getStages(int n){      

    char[] lockers = new char[n];

    char[][] arrayLockers = new char[n][];

    for (int i = 0; i < n; i++) {

      lockers[i] = 'O';

    }

    for (int i = 0; i<1;i++){

      arrayLockers[i] = lockers;

    }

    for(int i = 2; i<=n; i++){

      for(int z = 1; z<n; z++){

        for (int w = i-1; w <= n; w += i){

          lockers[w] = 'X'; 

        }

        arrayLockers[z] = lockers;

      }


    }

    return arrayLockers;

  }

}

输出适用于 n = 10:


> run Program11

Number of lockers: 10

Show stages [y/n]? y

OXXXXXXXXX

OXXXXXXXXX

OXXXXXXXXX

OXXXXXXXXX

OXXXXXXXXX

OXXXXXXXXX

OXXXXXXXXX

OXXXXXXXXX

OXXXXXXXXX

OXXXXXXXXX

更衣室问题如下:有 n 个学生和 n 个储物柜。第一个学生打开了所有的储物柜。第二个学生关闭储物柜 2,4,6,8,.... 第三个学生关闭储物柜 3,6,9,12,... 这种模式不断重复,直到所有 n 个学生都离开。


我的任务:


对于给定的 n,我应该在每个阶段展示储物柜。“X”代表封闭,“O”代表开放。我应该只使用数组。显然,这是一遍又一遍地重复相同的数组,这是不正确的。我使用getStages方法返回一个多维数组“arrayLockers”来存储每个阶段。谁能告诉我哪里出错了?


预期输出为:


OOOOOOOOOO   

OXOXOXOXOX   

OXXXOXXXXX   

OXXXOXXXXX  

OXXXXXXXXX  

OXXXXXXXXX  

OXXXXXXXXX    

OXXXXXXXXX   

OXXXXXXXXX    

OXXXXXXXXX   


守着一只汪
浏览 145回答 1
1回答

互换的青春

您的代码存在一些问题。首先,您没有arrayLockers在复制之前分配内存。其次,您使用三个for循环(嵌套)使逻辑复杂化。只需两个嵌套循环即可完成。另外,您使用的是数组分配,我不确定它是否有效;所以我改变了System.arraycopy()我经常使用的它(或者你可以使用for循环手动完成)。import java.util.*;import java.lang.*;import java.io.*;class Program11 {&nbsp; public static void main(String args[]) {&nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; System.out.print("Number of lockers:\n");&nbsp; &nbsp; int number = input.nextInt();&nbsp;&nbsp; &nbsp; System.out.print("Show stages [y/n]?\n");&nbsp; &nbsp; String show = input.next();&nbsp; &nbsp; if(show.equals("y"))&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; for (char[] lockers : getStages(number))&nbsp;&nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for (char c : lockers)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("" + c);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }&nbsp; public static char[][] getStages(int n){&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; char[] lockers = new char[n];&nbsp; &nbsp; char[][] arrayLockers = new char[n][];&nbsp; &nbsp; for (int i = 0; i < n; i++) {&nbsp; &nbsp; &nbsp; lockers[i] = 'O';&nbsp; &nbsp; }&nbsp; &nbsp; arrayLockers[0] = new char[lockers.length];&nbsp; &nbsp; System.arraycopy(lockers, 0, arrayLockers[0], 0, lockers.length);&nbsp; &nbsp; int cnt = 2;&nbsp; &nbsp; for (int i = 1; i < n; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = i; j < n; j += cnt) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lockers[j] = 'X';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; arrayLockers[i] = new char[lockers.length];&nbsp; &nbsp; &nbsp; &nbsp; System.arraycopy(lockers, 0, arrayLockers[i], 0, lockers.length);&nbsp; &nbsp; &nbsp; &nbsp; cnt++;&nbsp; &nbsp; }&nbsp; &nbsp; return arrayLockers;&nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答