【问题标题】:Reading multiple char inputs on one line在一行上读取多个字符输入
【发布时间】:2015-05-03 03:40:17
【问题描述】:

我遇到了一个难题,我只需要更改我的数组,以便将其作为每行一个字符串而不是单个字符读入;但是,当我这样做时,它会完全改变我的整个程序。如果需要,我可以提供更多代码。

我已经尝试过 hasNext 以及我能想到的所有其他方法。我在下面放了一个例子来说明我的意思

如果用户输入:2

用户想输入O,X,O,X,但我想这样做,这样他们仍然可以输入字符,但每行有多个字符。 我希望他们能够在一行输入OX,在下一行输入OX

package mazeanalyze.java;
import java.util.*;

/**
 *
 * @author RexABBWJonathan
 */
public class MazeAnalyzeJava {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    // TODO code application logic here
    Scanner kbd = new Scanner(System.in);
    //int[][] totalOpenness = new int[n][n];
    System.out.println("ENTER A SINGLE INTEGER: ");
    int n = kbd.nextInt();
    int[][] totalOpenness = new int[n+1][n+1];
    char[][] mazeValue = new char[n + 1][n + 1];
    System.out.println("ENTER A PATH: ");

    for (int i = 0; i < mazeValue.length; i++) {
        for (int j = 0; j < mazeValue[i].length; j++) {
            if (i == 0 || j == 0 || i == n + 1 || j == n + 1)
                mazeValue[i][j] = 'X';
            else {
                mazeValue[i][j] = kbd.next().charAt(0);
            }
        }
    }
    printMaze(mazeValue);
    System.out.println("");
    openfactor(mazeValue, n);
    System.out.println("");
    horizontalPath(mazeValue, n);
    System.out.println(" ");
    verticalPath(mazeValue,n);
    System.out.println(" ");
    //openfactor(mazeValue, n);
    //average(mazeValue,n,totalOpenness);
}
    public static void printMaze(char mazeValue[][]) {
    System.out.println("MAZE");
    for (int i = 1; i < mazeValue.length; i++) {
        for (int j = 1; j < mazeValue[i].length; j++) {
            System.out.printf("%5c", mazeValue[i][j]);
        }
        System.out.printf("\n");
    }
}

   public static void horizontalPath(char mazeValue[][], int n) {
    int[] totalRow = new int[n];

    // int horizontalPath=0;
    int count = 0;
    for (int i = 1; i < mazeValue.length; i++) {
        for (int j = 1; j < mazeValue[i].length; j++) {
            if (mazeValue[i][j] == 'O') {
                count++;
            } else {
                if (totalRow[i - 1] < count)
                    totalRow[i - 1] = count;
                count = 0;
            }
        }
        if (totalRow[i - 1] < count)
            totalRow[i - 1] = count;
        count = 0;
    }
    int biggestRow = totalRow[0];
    // int longestRow=0;
    int finalLongestRow = 0;
    for (int x = 0; x < n; x++) {
        if (biggestRow < totalRow[x]) {
            biggestRow = totalRow[x];
            finalLongestRow = x;
        }
    }
    System.out.printf("Longest horizontal path row %d length %d", finalLongestRow + 1, biggestRow);
}
public static void verticalPath(char mazeValue[][], int n) {
    int[] totalColumn = new int[n];

    // int horizontalPath=0;
    int count = 0;

    for (int j = 1; j < mazeValue.length; j++){
        for (int i = 1; i < mazeValue[j].length; i++) {

            if (mazeValue[i][j] == 'O') {
                count++;
            } else {
                if (totalColumn[j - 1] < count)
                    totalColumn[j - 1] = count;
                count = 0;
            }
        }
        if (totalColumn[j - 1] < count)
            totalColumn[j - 1] = count;
        count = 0;
    }
    int biggestColumn = totalColumn[0];
    // int longestRow=0;
    int finalLongestColumn = 0;
    for (int x = 0; x < n; x++) {
        if (biggestColumn < totalColumn[x]) {
            biggestColumn = totalColumn[x];
            finalLongestColumn = x;
        }
    }
    //System.out.printf("Longest horizontal path row %d length %d", finalLongestColumn + 1, biggestColumn);

   System.out.printf("Longest vertical path column %d length %d", finalLongestColumn + 1, biggestColumn);

}
   public static void openfactor(char[][] mazeValue, int n)
       {
           //double rowAvg=0;
           //double totalRowAvg=0;
       int[][] totalOpenness = new int[n+1][n+1];
       System.out.println("STATISTICS");
       for(int i = 1; i<=n; i++)
       {  
           double rowAvg=0;
           double totalRowAvg=0;

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

              int count=0;
              int openness=0;
              int totalRowOpeness = 0;
              //double rowAvg=0;

               if(mazeValue[i][j]=='X'){
                   count--;
               }

              else 
               {
               //YOU NEED TO VERIFY THAT J IS NOT OUT OF BOUND
               if( j-1>=1)
                    {
               if(mazeValue[i][j-1]=='O')
                        count++;
                    }
                      // System.out.println("cout: "+count);

                    if(i-1>=1 && j-1>=1)
                    {
                    if(mazeValue[i-1][j-1]=='O')
                        count++;
                    }
                     //  System.out.println("cout: "+count);
                     if(i-1>=1)
                    {
                    if(mazeValue[i-1][j]=='O')
                        count++;
                     }
                    //   System.out.println("cout: "+count);
                    if(j+1<=n)
                    {
                    if(mazeValue[i][j+1]=='O')
                        count++;
                    }
                     //  System.out.println("cout: "+count);
                    if(j+1<=n && i+1<=n)
                    {
                    if(mazeValue[i+1][j+1]=='O')
                        count++;
                    }
                    if (i+1<=n)
                    {
                    if(mazeValue[i+1][j]=='O')
                        count++;
                    }
                    //   System.out.println("cout: "+count);
                    if(j-1>=1 && i+1<=n)
                    {
                    if(mazeValue[i+1][j-1]=='O')
                        count++;
                    }
                    if(i-1>=1 && j+1<=n)
                    {
                    if(mazeValue[i-1][j+1]=='O')
                        count++;
                    }
            //}//eND OF iF CONDITION\
            }
            openness = openness +count;
            totalOpenness[i][j] = openness;
            //System.out.println("TOTAL OPENESS FOR : [" + i + "]" +"[" + j + "]  IS " +totalOpenness[i][j]);
            System.out.printf("%5d",totalOpenness[i][j]);
            totalRowOpeness = totalRowOpeness + totalOpenness[i][j];
            rowAvg = (double)totalRowOpeness/(double)n;
            totalRowAvg = totalRowAvg + rowAvg;
         }
        //System.out.println("TOTAL SUM ROW AVERAGE: " +totalRowAvg);
        System.out.printf("%5.1f",totalRowAvg);
        System.out.println("");
    }

    for(int j=1; j<=n;j++){
        double ColumnAvg=0;
        double totalColumnAvg=0;
        for(int i=1; i<=n;i++){
            int count=0;
            int openness=0;
            int totalColumnOpeness = 0;

            if(mazeValue[i][j]=='X'){
                   count--;
               }

              else 
               {
               //YOU NEED TO VERIFY THAT J IS NOT OUT OF BOUND
               if( j-1>=1)
                    {
               if(mazeValue[i][j-1]=='O')
                        count++;
                    }
                      // System.out.println("cout: "+count);

                    if(i-1>=1 && j-1>=1)
                    {
                    if(mazeValue[i-1][j-1]=='O')
                        count++;
                    }
                     //  System.out.println("cout: "+count);
                     if(i-1>=1)
                    {
                    if(mazeValue[i-1][j]=='O')
                        count++;
                     }
                    //   System.out.println("cout: "+count);
                    if(j+1<=n)
                    {
                    if(mazeValue[i][j+1]=='O')
                        count++;
                    }
                     //  System.out.println("cout: "+count);
                    if(j+1<=n && i+1<=n)
                    {
                    if(mazeValue[i+1][j+1]=='O')
                        count++;
                    }
                    if (i+1<=n)
                    {
                    if(mazeValue[i+1][j]=='O')
                        count++;
                    }
                    //   System.out.println("cout: "+count);
                    if(j-1>=1 && i+1<=n)
                    {
                    if(mazeValue[i+1][j-1]=='O')
                        count++;
                    }
                    if(i-1>=1 && j+1<=n)
                    {
                    if(mazeValue[i-1][j+1]=='O')
                        count++;
                    }
            //}//eND OF iF CONDITION\
            } 
            openness = openness +count;
            totalOpenness[j][i] = openness;
            //System.out.println("TOTAL OPENESS FOR : [" + j + "]" +"[" + i + "]  IS " +totalOpenness[j][i]);
            totalColumnOpeness = totalColumnOpeness + totalOpenness[j][i];
            ColumnAvg = (double)totalColumnOpeness/(double)n;
            totalColumnAvg = totalColumnAvg + ColumnAvg;
        }
        //System.out.println("TOTAL SUM COLUMN AVERAGE: " +totalColumnAvg);
        System.out.printf("%5.1f",totalColumnAvg);

    }

  }
}

【问题讨论】:

  • however when I do so it completely changes my whole program 你必须很多更具体。
  • 我试图创建一个字符串 STR 并做 mazeValue[i][j] = STR.charAt(j);它奏效了,但在我的程序中提升了我所有的计数和平均值。
  • 我已经更新了我的代码,以便您可以看到更多。我不知道如何以正确的方式改变它。
  • 好的,如果我理解得很好,您必须 1/ 使用 readLine() 2/ 将行分成 char 并取 N(N 是用户输入的值)。您是在问如何执行第 2 步吗?
  • 您提到的计数和平均值。您在创建迷宫时指的是 for 循环吗?如果你想做 String.charAt(x) 的想法,你只需要创建一个单独的计数器,并在每次得到一个字符后递增它。当你到达字符串的末尾时,让他们输入一个新的。

标签: java arrays string for-loop char


【解决方案1】:

您遇到的问题是您正在调用“kbd.next().charAt(0);”。问题在于 .next() 获取一个字符串(在您的情况下为“oxxox”或其他内容),然后只获取第一个字符,因此之后的所有内容都将丢失。

我不知道用扫描仪读取字符的另一种方法。所以下面是我建议的 main 代码。它所做的只是调用“.next()”然后遍历它。因为计数器 i 和 j 的操作与我们想要的不同。我们需要创建一个手动递增的本地计数器。

public static void main(String[] args) {
    // TODO code application logic here
    Scanner kbd = new Scanner(System.in);
    // int[][] totalOpenness = new int[n][n];
    System.out.println("ENTER A SINGLE INTEGER: ");
    int n = kbd.nextInt();
    int[][] totalOpenness = new int[n + 1][n + 1];
    char[][] mazeValue = new char[n + 1][n + 1];
    System.out.println("ENTER A PATH: ");

    String input = ""; //Where we store kbd.next()
    int charPosition = 0; //Our manual counter

    for (int i = 0; i < mazeValue.length; i++) {
        for (int j = 0; j < mazeValue[i].length; j++) {
            if (i == 0 || j == 0 || i == n + 1 || j == n + 1)
                mazeValue[i][j] = 'X';
            else {
                while( input.equals("") || input.length() <= charPosition )
                {
                    input = kbd.next();
                    charPosition = 0;
                }
                mazeValue[i][j] = input.charAt(charPosition);
                charPosition++;
            }
        }
    }
    printMaze(mazeValue);
    System.out.println("");
    openfactor(mazeValue, n);
    System.out.println("");
    horizontalPath(mazeValue, n);
    System.out.println(" ");
    verticalPath(mazeValue,n);
    System.out.println(" ");
    openfactor(mazeValue, n);
    average(mazeValue,n,totalOpenness);
}

【讨论】:

  • 老兄,你不知道你刚刚救了我多少。我觉得我把那部分搞砸了,我妥协了我的整个程序。你是一个可怕的生活SAVERRRRRRRR。
  • @FredrickWilliams 没问题,很高兴我能帮上忙,希望你学到新东西 :)
猜你喜欢
  • 2021-07-16
  • 1970-01-01
  • 2013-10-20
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 2017-09-07
相关资源
最近更新 更多