【问题标题】:How to process an array and store it in java?如何处理数组并将其存储在java中?
【发布时间】:2021-02-07 16:47:44
【问题描述】:

我想基本上使用数组将教室座位分配给学生。因此,我想将该信息与学生姓名一起保存到不同的文本文件中,并且我设法用学生的姓名做到了这一点,但我无法分配座位,因为我不确定如何使用单个数组。我基本上希望程序使用 (1)(1)、(1)(2)...等列和行来分配座位。如果是 3x10 的教室,那么行数限制应该是 3,列数应该是 10,但是如果教室大小是 6x5,那么应该有 6 行 5 列。所以根据用户选择的教室,数组(分配座位)应该改变。

示例输入:

Select Your Class Size!

A 6x5 Classroom or a 3X10 classroom?
 Enter '6x5' or '3x10' please!

6x5

Ok, so you have selected 6x5
Your classroom size looks like this:

XXXXXX
XXXXXX
XXXXXX
XXXXXX
XXXXXX

Now Enter The Number Of Students!
3

Enter the names of the 3 students!

George
Paul
Jacob

The Student Names And Seat Location Are As Follows:
George Seat Location:
Paul Seat Location:
Jacob Seat Location:

样本输出:

选择你的班级人数!

A 6x5 Classroom or a 3X10 classroom?
 Enter '6x5' or '3x10' please!

6x5

Ok, so you have selected 6x5
Your classroom size looks like this:

XXXXXX
XXXXXX
XXXXXX
XXXXXX
XXXXXX

Now Enter The Number Of Students!
3

Enter the names of the 3 students!

George
Paul
Jacob

The Student Names And Seat Location Are As Follows:
George Seat Location: (1)(1)
Paul Seat Location: (1)(2)
Jacob Seat Location: (1)(3)

注意:如果用户选择 3x10 的教室,那么座位位置会相应改变,因为行数和列数会有限制。如果我选择 3x10 教室,在 (1)(3) 之后,下一个座位位置将是 (2)(1),然后是 (2)(2),(2)(3),然后它将进入下一列 = => (3)(1)...等,直到达到最大列。您能帮我创建执行此操作的数组吗?人名和座位位置应该保存在名为StudentNames的纺织品中注意:学生姓名已经保存,我只是在座位位置上挣扎

代码:

// Import scanner
import java.util.Scanner;
import java.io.*;


// Create a class and method
class Main {
  public static void main(String[] args) {

    // Clear the screen
    System.out.print("\033[H\033[2J");
    System.out.flush();

    // Create scanner object
    Scanner inp = new Scanner(System.in);

    // Create a print statement
    System.out.println("Select Your Class Size!\n");
    System.out.println("A 6x5 Classroom or a 3X10 classroom?\n Enter '6x5' or '3x10' please!\n");

    String Class1 = "6x5";
    String Class2 = "3x10";

    Double input[] = new Double[1];

    String selectClassSize = inp.next();

    int indexOfx = selectClassSize.indexOf('x');
    int xcount = 0;

    boolean containsx = indexOfx == 0 || indexOfx == (selectClassSize.length() - 2);

    if (containsx) {
      input[xcount] = Double.parseDouble(selectClassSize.replace("x", ""));
      
      System.out.println("\nOk, so you have selected " + Class1);
      System.out.println("Your classroom size looks like this:\n");

      int rows = 6;
      int columns = 5;
      int classSize[][] = new int [rows][columns];
      
      for(int i = 0; i < classSize[0].length; i++){
        for(int j = 0; j < classSize.length; j++){
            System.out.print("X");
      }
      System.out.println();
    }

      xcount++;

    } else {
      System.out.println("\nOk, so you have selected " + Class2);
      System.out.println("Your classroom size looks like this:\n");

      int rows2 = 3;
      int columns2 = 10;
      int classSize2[][] = new int [rows2][columns2];
        for(int x = 0; x < classSize2[0].length; x++){
          for(int y = 0; y < classSize2.length; y++){
              System.out.print("X");
      }
      System.out.println();
      }
    }

    // Create a scanner variable

    System.out.println("\nNow Enter The Number Of Students!");

    int numOfStudents = inp.nextInt();

    // Create a counter variable to count upto the numOfStudents and break the loop
    int counter = 0;

    System.out.println("\nEnter the names of the " + numOfStudents + " students!\n");


     try {

      // Initialize the new objects
      FileWriter fw = new FileWriter("StudentNames");
      BufferedWriter bw = new BufferedWriter(fw);

      String[] names = new String[numOfStudents];

      // Output the first names in the textfile
      for (int x = 0; x < numOfStudents; x++) {
        names[x] = inp.next();
        bw.write(names[x]+"Seat Location:");
        bw.newLine();
      }

      bw.close();
      fw.close();

      // Catch any errors
    } catch (Exception e) {
      System.out.println("An Error Occured!");
    }
     try {

      // Initialize the new objects
      FileReader fr = new FileReader("StudentNames");
      BufferedReader br = new BufferedReader(fr);

      String line = br.readLine();

      // Start a while loop to output the data from the file
      System.out.println("\nThe Student Names And Seat Location Are As Follows:");
      
      while (line != null) {
        System.out.println(line);
        line = br.readLine();

      }

      br.close();
      fr.close();

      // Catch any errors
    } catch (Exception e1) {
      System.out.println("An Error Occured!");
    }
  }
}

【问题讨论】:

    标签: java arrays sorting for-loop multidimensional-array


    【解决方案1】:

    嗨:D ermm 我不知道我的版本是否适合你,但我希望它是正确的......所以我的概念是我为座位创建一个数组,然后我只是将所有座位矩阵编号存储到数组中(因为你没有提到座位号是否需要随机分配给每个学生)所以我只是根据学生,它会打印出座位号(列和行)。让我知道我是否错了,祝你有美好的一天:D

    import java.util.Scanner;
    import java.io.*;
    
    
    // Create a class and method
    public class grages {
      public static void main(String[] args) {
    
        // Clear the screen
        System.out.print("\033[H\033[2J");
        System.out.flush();
    
        // Create scanner object
        Scanner inp = new Scanner(System.in);
    
        // Create a print statement
        System.out.println("Select Your Class Size!\n");
        System.out.println("A 6x5 Classroom or a 3X10 classroom?\n Enter '6x5' or '3x10' please!\n");
    
        String Class1 = "6x5";
        String Class2 = "3x10";
    
        Double input[] = new Double[1];
    
        String selectClassSize = inp.next();
    
        int indexOfx = selectClassSize.indexOf('x');
        int xcount = 0;
    
        boolean containsx = indexOfx == 0 || indexOfx == (selectClassSize.length() - 2);
    
        if (containsx) {
          input[xcount] = Double.parseDouble(selectClassSize.replace("x", ""));
          
          System.out.println("\nOk, so you have selected " + Class1);
          System.out.println("Your classroom size looks like this:\n");
    
          int rows = 6;
          int columns = 5;
          int classSize[][] = new int [rows][columns];
          
          for(int i = 0; i < classSize[0].length; i++){
            for(int j = 0; j < classSize.length; j++){
                System.out.print("X");
          }
          System.out.println();
        }
          xcount++;
    
        } else {
          System.out.println("\nOk, so you have selected " + Class2);
          System.out.println("Your classroom size looks like this:\n");
    
          int rows2 = 3;
          int columns2 = 10;
          int classSize2[][] = new int [rows2][columns2];
            for(int x = 0; x < classSize2[0].length; x++){
              for(int y = 0; y < classSize2.length; y++){
                  System.out.print("X");
          }
          System.out.println();
          }
        }
    
        // Create a scanner variable
    
        System.out.println("\nNow Enter The Number Of Students!");
    
        int numOfStudents = inp.nextInt();
    
        // Create a counter variable to count upto the numOfStudents and break the loop
        int counter = 0;
    
        System.out.println("\nEnter the names of the " + numOfStudents + " students!\n");
         try {
    
          // Initialize the new objects
          FileWriter fw = new FileWriter("StudentNames");
          BufferedWriter bw = new BufferedWriter(fw);
    
          String[] names = new String[numOfStudents];
          String[] seats = new String[numOfStudents];
          
          int row=1 , column = 0;
          
          // Output the first names in the 
          for (int x = 0; x < numOfStudents; x++) {
              names[x] = inp.next();
              if(containsx) {
                  if(column>=6) {
                      ++row;
                      column = 0;
                  }
              }
              else {
                  if(column>=3) {
                      ++row;
                      column = 0;
                  }
              }
              seats[x] = "("+Integer.toString(row)+")" + "("+Integer.toString(++column)+")";
              bw.write(names[x]+" Seat Location:"+seats[x]);
              bw.newLine();
          }
    
          bw.close();
          fw.close();
    
          // Catch any errors
        } catch (Exception e) {
          System.out.println("An Error Occured!");
        }
         try {
    
          // Initialize the new objects
          FileReader fr = new FileReader("StudentNames");
          BufferedReader br = new BufferedReader(fr);
    
          String line = br.readLine();
    
          // Start a while loop to output the data from the file
          System.out.println("\nThe Student Names And Seat Location Are As Follows:");
          
          while (line != null) {
              System.out.println(line);
              line = br.readLine();
    
            }
    
          br.close();
          fr.close();
    
          // Catch any errors
        } catch (Exception e1) {
          System.out.println("An Error Occured!");
        }
      }
    }
    

    【讨论】:

    • 如果我只想对名称进行排序并使用 Arrays.sort() 将其写入纺织品,我该怎么做?
    猜你喜欢
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    相关资源
    最近更新 更多