Java - 用户字符串输入转换为6x6的2D数组

时间:2017-03-27 08:35:53

标签: java arrays

我对以下场景的Java代码实现有疑问,

  1. 获取用户输入[来自扫描仪的字符串]
  2. 将String输入转换为1D Array
  3. 从1D char数组中,根据位置相应地填充2D char数组, [0] [0],[0] [1]等
  4. 我所做的如下,

    charArray已创建为6乘6阵列。<?php error_reporting(); ini_set('display_errors', 1); setlocale(LC_TIME, "de_DE"); class Calendar{ private $month; private $year; private $daysofweek; private $numdays; private $date_info; private $day_of_week; private $tnr; public function __construct($month,$year,$days_of_week =array('So','Mo','Di','Mi','Do','Fr','Sa')){ $this->month =$month; $this->year =$year; $this->days_of_week =$days_of_week; $this->num_days =cal_days_in_month(CAL_GREGORIAN, $this->month, $this->year); $this->date_info =getdate(strtotime('first day of', mktime(0,0,0,$this->month,1,$this->year))); $this->day_of_week =$this->date_info['wday']; $this->monat = date("n", mktime(0, 0, 0, $this->month, 1, $this->year)); } public function show($tnr) { $monate = array(1=>"Januar", 2=>"Februar", 3=>"M&auml;rz", 4=>"April", 5=>"Mai", 6=>"Juni", 7=>"Juli", 8=>"August", 9=>"September", 10=>"Oktober", 11=>"November", 12=>"Dezember"); include '../include/myt.php'; // WERT 0 entfernen // GRÖße des ARRAY $size =sizeof($tnr); $now_mon= $this->monat; foreach($tnr as $tnrs){ $sql= "SELECT * FROM termin WHERE nr = '$tnrs' AND monat ='$now_mon' "; $query = mysqli_query($conn, $sql); $row = mysqli_fetch_array($query); $tagdata = $row['tag']; if($tagdata == ""){ ""; } else { $tage[] = $tagdata; } } $output= '<table id="table'. $this->monat . '" class="table table-striped">'; $output .= '<thead id="head'. $this->monat . '" style="text-align:center">'. $monate[$this->monat] . ' ' . $this->year . '</thead>'; $output .= '<tr>'; foreach( $this->days_of_week as $day) { $output .= '<th class="header center">' . $day . '</th>'; } $output .= '</tr><tr>'; if($this->day_of_week > 0){ $output .= '<td colspan="' . $this->day_of_week . '"></td>'; } $current_day =1; while ( $current_day <= $this->num_days){ if($this->day_of_week ==7){ $this ->day_of_week =0; $output .= '</tr></tr>'; } ///PROBLEM print_r($tage); foreach($tage as $tag){ if($current_day == $tag){ $current='style ="background: black;"'; } else { $current= ''; } } ///PROBLEM $output .='<td class="day"'.$current.'>' . $current_day . '</td>'; $current_day++; $this->day_of_week++; } if($this->day_of_week != 7){ $remaining_days = 7 - $this->day_of_week; $output .= '<td colspan="' . $remaining_days . '"></td>'; } $output .= '</tr>'; $output .= '</table>'; echo $output; } } ?>

    char[][] charArray = new char[6][6];

    从消息的打印看,我可以得到文本的i位置和相应的2D数组位置。但我无法通过分配来填充值。

    我感谢上述案例的任何提示和指导。

    非常感谢。

2 个答案:

答案 0 :(得分:0)

我没有得到你的问题。 但是如果你想存储在6 * 6字符数组中而不会出现异常。 这是代码怎么做

    Scanner sc = new Scanner(System.in);
    String s = sc.nextLine();
    sc.close();
    s=s.replaceAll("\\s+", "");
    char []ch = s.toCharArray();
    char [][]charArray = new char[6][6];
    int i=0, j=0;

    for(int k=0;k<ch.length;k++)
    {


        if(i<=5 && j<=5)charArray[i][j] = ch[k];
        j++;
        if(j>5) {
            j=0;
            i++;
        }
        if(i>5 && j>5) break;

    }

答案 1 :(得分:0)

最简单的方法是确保用户完全输入了36个字符,然后您就可以复制char数组。

Live Demo

Scanner scn = new Scanner (System.in);
System.out.print("String text: ");
String str = "";

// Read 6x6 characters
while (str.toCharArray().length != 36)
{
        System.out.print("Enter 36 characters: ");
        str = scn.nextLine().replaceAll("\\s+","");
}

char[] cArr = str.toCharArray();

// Print in 6x6 grid
for(int i=0; i<36; i++)
{
    System.out.print((i%6==0?"\n ":" ")+cArr[i]);
}
System.out.println();

您可以将该1D数组用作平面2D数组,或者以这种方式将它们解析为2D数组。

System.out.println("-------------------");

char[][] cMat = new char[6][6];

// Parse it into 6x6 matrix
for(int i=0; i<36; i++)
{
    cMat[i/6][i%6] = cArr[i];
}

// Print 6x6 matrix
for(int i=0; i<6; i++)
{
    for(int j=0; j<6; j++)
    {
        System.out.print(" "+cMat[i][j]);
    }
    System.out.println();
}