【问题标题】:'.class' expected error java and arrays'.class' 预期错误 java 和数组
【发布时间】:2015-02-18 00:07:13
【问题描述】:

我正在尝试将以下数字文件转换为其字符,然后打印出来。然后,打印它翻转。 它是 Homer Simpson 的图像,目前第一种方法是在第一个 return 语句时得到“Homer.java:38: error: '.class' expected”错误。

22 40 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 44 45 45 45 46 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 44 46 39 45 46 32 32 32 92 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 40 32 40 32 44 39 34 34 34 34 34 45 46 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 96 44 88 32 32 32 32 32 32 32 32 32 32 96 46 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 47 96 32 96 32 32 32 32 32 32 32 32 32 32 32 96 46 95 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 40 32 32 32 32 32 32 32 32 32 32 32 32 44 32 32 32 44 95 92 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 124 32 32 32 32 32 32 32 32 32 32 44 45 45 45 46 44 39 111 32 96 46 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 124 32 32 32 32 32 32 32 32 32 47 32 111 32 32 32 92 32 32 32 32 32 41 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 92 32 44 46 32 32 32 32 40 32 32 32 32 32 32 46 95 95 95 95 44 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 92 124 32 92 32 32 32 32 92 95 95 95 95 44 39 32 32 32 32 32 92 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 39 96 39 92 32 32 92 32 32 32 32 32 32 32 32 95 44 95 95 95 95 44 39 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 92 32 32 44 45 45 32 32 32 32 32 32 44 45 39 32 32 32 32 32 92 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 40 32 67 32 32 32 32 32 44 39 32 32 32 32 32 32 32 32 32 92 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 96 45 45 39 32 32 46 39 32 32 32 32 32 32 32 32 32 32 32 124 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 124 32 32 32 124 32 32 32 32 32 32 32 32 32 46 79 32 124 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 95 95 124 32 32 32 32 92 32 32 32 32 32 32 32 32 44 45 39 95 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 47 32 96 76 32 32 32 32 32 96 46 95 32 32 95 44 39 32 32 39 32 96 46 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 47 32 32 32 32 96 45 45 46 46 95 32 32 96 39 44 46 32 32 32 95 92 32 32 96 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 96 45 46 32 32 32 32 32 32 32 47 92 32 32 124 32 96 46 32 40 32 44 92 32 32 92 32 32 32 32 32 32 32 32 32 32 32 32 32 95 47 32 32 96 45 46 95 32 32 47 32 32 92 32 124 45 45 39 32 32 40 32 32 32 32 32 92 32 32 32 32 32 32 32 32 32 32 32 39 32 32 96 45 46 32 32 32 96 39 32 32 32 32 92 47 92 96 46 32 32 32 96 46 32 32 32 32 41 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 92 32 32 45 104 114 114 45 32 32 32 32 92 32 96 46 32 32 124 32 32 32 32 124 32 

这是我的代码:

    import java.util.*;
import java.io.*;

public class Homer{
   public static void main(String[] args)throws FileNotFoundException{
      Scanner homerFile = new Scanner(new File("homer2.txt" ));
      Scanner mysteryFile = new Scanner(new File("mystery2.txt" ));
      char[][] arr = makeHomer(homerFile);
     // arr = makeHomer(mysteryFile);

      char[][] newArr = Arrays.copyOf(arr);
      displayFlippedHoriz(homerFile,newArr);
   }

   public static char[][] makeHomer(Scanner file)throws FileNotFoundException{
      file.hasNext();
      int height = file.nextInt();
      int width = file.nextInt();
      char [][] arr = new char [height][width];

      while (file.hasNext()){
         for (int rows=0; rows<width; rows++){
            for (int col = 0; col< height; col++){
               int currNum = file.nextInt();
               char character = (char)currNum;
               arr[rows][col] = character;          
               System.out.print(character);

               if(rows+1 == height){
                 System.out.println();
               }

            }
         }

      }
      return arr[][];

   }

   public static void displayFlippedHoriz(Scanner file, char newArr[][]){

      while (file.hasNext()){
         for (int rows = 0; rows< height; rows++){
            for (int col = arr.length; col<width; col--){
               currNum = file.nextInt();
               character = (char)currNum;
               System.out.print(character);
            }
         }
        if (rows+1 == height){
            System.out.println();
        }
      }

   }
}

【问题讨论】:

  • 你认为arr[][]是什么意思?

标签: java arrays for-loop methods while-loop


【解决方案1】:

要在声明后引用数组本身,不需要[] 字符。只需使用:

return arr;

.class 出现错误消息的原因是,从 arr[][] 生成有效表达式的唯一方法是从中生成类文字,例如arr[][].class。这没有用,因为 arr 无论如何都不是类名。

【讨论】:

    猜你喜欢
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    相关资源
    最近更新 更多