【问题标题】:trying to print chars but still prints string尝试打印字符但仍打印字符串
【发布时间】:2013-04-13 12:56:57
【问题描述】:

我正在尝试从 2d 字符数组中打印字符,但是我不断返回一个字符串而不是每个字母。例如命令行中的参数是

"-加密 abcdefghij"

    String command = args[0]; 
    String Text = args[1]; //letters to be 

    char letters [] = Text.toCharArray(); 
    int m = Text.length(); //number of letters to be decrypted/encrypted


    String command = args[0]; 
    String Text = args[1]; //leters to be 

    char letters [] = Text.toCharArray(); 
    int m = Text.length(); //number of letters to be decrypted/encrypted


    if (command.equals("-encrypt")) {

        //if statement was here before for perfect square numbers

        else if     ( m / (int) Math.sqrt(m) != Math.sqrt(m) ) { //non perfect square digits

        int RootM = (int) Math.pow((Math.sqrt(m))+1,2); //overall size of 2d array (depth*width)
        int RootN1 = (int) Math.sqrt(RootM); //length of rows & columns

        char [][] box = new char [RootN1][RootN1]; //define dimensions of 2d array

            for (int i=0; i<RootN1; i++) {
                for (int j=0; j<RootN1; j++) {
                    box[i][j]=letters[2*i+j];
                }
            }

            for (int i=0; i<RootN1; i++) {
                for (int j=0; j<RootN1; j++) {  

            System.out.print(box[j][i]); 

【问题讨论】:

  • box 是一个二维数组,但在分配和打印时您将它用作一维数组。也许这会导致混乱?
  • 我试图使用字符来填充二维数组,但我一直得到输出 abcd 而不是 a,b,c,d

标签: java string char


【解决方案1】:

您在级联 for 循环中执行的操作如下:

box[0] = "abcd";
box[0] = "abcd";
box[0] = "abcd";
box[1] = "abcd";
box[1] = "abcd";
box[0] = "abcd";
...

我不完全知道您是如何尝试填充该二维数组的,但我想您将不得不执行类似于 box[i][j] = letters[2*i+j] 的操作。

【讨论】:

    【解决方案2】:
     for (int i=0; i<RootM; i++) {
        for (int j=0; j<RootM; j++) {
          box [i] = letters;
          box [j] = letters;
          System.out.print(box[i]); //displays encrypted text
        }
     }
    

    有问题。您将box[i] = letters 放在内部循环中,因此它将执行RootM*RootM 次,这与print 语句相同。您可能需要清除 box 2D 字符数组上的分配和打印逻辑。

    根据您对cmets的回复,您需要将abcd放入2*2矩阵,第一行包含ab,第二行包含cd。您可以找到索引映射如下:

    index in 1D array:
        0    1    2  3
    
    index in 2D array:
    
      (0,0) (0,1) (1,0) (1,1)
    

    所以映射是:

    box[i][j] = letters[2*i+j];
    

    现在假设您有一个 4*4 矩阵:

    index in 1D array:
    0  1  2  3  4 5  6  7  8  9  10  11  12  13  14  15
    
    index in 2D array:
    (0,0),(0,1),(0,2),(0,3),(1,0),(1,2) ....
    

    你需要映射

    box[i][j] = letters[4*i+j];
    

    简而言之,如果行数为N,则需要

    box[i][j] = letters[N*i + j];
    

    你的循环应该是这样的:

    for (int i=0; i<RootM; i++) {
      for (int j=0; j<RootM; j++) {
        box [i][j] = letters[2*i+j];
      }
    }
    
    //you can now print column by column
    for (int i=0; i<RootM; i++) {
      for (int j=0; j<RootM; j++) {
         System.out.print(box [j][i]);
      }
    }
    

    【讨论】:

    • 您也知道如何将字符串文本更改为 char where 以便打印 acbd 吗?
    • 如果数组是逐行填充的,即第 1 行包含 |a|b| AND 第 2 行包含 |c|d|
    • 映射是什么意思?我是这个 java 东西的新手:/
    • 呃一个问题我得到了输出“abcd”\n“a bd”怎么c被取消了?
    • for (int i=0; i
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 2021-07-22
    • 2014-05-28
    相关资源
    最近更新 更多