【问题标题】:Various errors while trying to use variables as parameters尝试将变量用作参数时出现各种错误
【发布时间】:2020-04-10 11:47:28
【问题描述】:

我正在编写一个程序来输入一个包含三个不同字符的 50 x 50 块的文本文件: # , * 和 / ,并根据每个符号为输出 PNG 文件的每个像素着色,以便每个字符都有一种颜色。但是,我遇到了几个与将变量作为参数传递相关的语法错误。下面列出了这些语法错误:

Syntax error on token ",", < expected [21 , 59]
Syntax error, insert "Dimensions" to complete ArrayType [22,5]
Syntax error, insert ">" to complete ReferenceType1 [22,5]
Syntax error on token ",", delete this token [88, 53]
Syntax error, insert "... VariableDeclaratorId" to complete FormalParameter [88,69]
image_width cannot be resolved to a variable [90, 40]
image_height cannot be resolved to a variable [90, 53]
input_file_path cannot be resolved to a variable [92 , 59]

有人可以告诉我如何正确传递这些参数吗?将不胜感激。

这是程序代码:

import java.util.Scanner;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter; 

public class Lab_Week8_ImgFrmTxt_Part2 {

    public static void main(String[] args) throws Exception  {  

    int image_width = 50;
    int image_height = 50;
    String output_file_path = ("image.png");
    String input_file_path = "superimage.txt";

    }

    public static BufferedImage imageFromText (image_width, image_height, input_file_path, output_file_path, 
    int r, int g, int b, char[][] characters) throws IOException{

        if(image_width <= 0 || image_height <= 0) {
            System.err.println("Width and Height have to be strictly positive!");
            return null;
        }

        BufferedImage image = new BufferedImage (image_width, image_height, BufferedImage.TYPE_INT_ARGB);

        BufferedImage imagetowrite = imageFromText(image_width, image_height, input_file_path, output_file_path, r, g, b, characters); 
        File f = new File (output_file_path);
        ImageIO.write(imagetowrite, "png", f);

        int x = 0;
        int y = 0;
        for (x = 0; x < image_width; x++)
        {
        for (y = 0; y < image_height; y++){

            if (characters[x][y] == ('#'))
            {r = 255;
            b = 0;
            g = 0;
            }

            else if (characters[x][y] == ('/'))
            {r = 0;
            b = 255;
            g = 0;
            }

            else if (characters[x][y] == ('*'))
            {r = 0;
            b = 0;
            g = 255;
            }
            setPixel(image, x, y, r, g, b); }
        }
        return image;
    }

    public static void setPixel(BufferedImage image, int x, int y, int r, int g, int b) {

        if(x < 0 || y < 0) {
            System.err.println("Coordinates (x, y) cannot be negative numbers!");
            return;
        }
        if(r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
            System.err.println("Colour values (r, g, b) have to be between 0 and 255!");
            return;
        }

        int a = 255; //alpha value
        /*
         * Write the different value of the pixel, i.e. the colours red, green, blue and alpha.
         * The different colour values are all stored into a single integer using byte operators.
         * a = x << y means write the value of x as bytes at the address y in the object a.
         * a = o1 | o2 means a will be composed by o1 and o2, where o1 and o2 are binary operators.
         * It is necessary to use this operator because the setRGB method of BufferedImage 
         * take only one integer that have to hold all the colour values for one given pixel.
         */
        int p = (a << 24) | (r << 16) | (g << 8) | b;

        image.setRGB(x, y, p);
    }

    public static char[][] readTextImage(image_width, image_height, input_file_path) throws FileNotFoundException {  
        /* Create a 2-dimensional array of characters to store the data from the file */  
        char[][] characters = new char[image_width][image_height]; 

        Scanner reader = new Scanner (new FileInputStream(input_file_path));

        /* Set x and y variable at 0 to keep track of the coordinates */  
        int x = 0; 
        int y = 0;  
        /* While there is something to read in the file */  
        while (reader.hasNextLine()) {   
            String s = reader.nextLine();  /* Grab the next line */   
            for (int i = 0; i < s.toCharArray().length; i++) {   /* For each character in the current line */    
                /* Get the current character (use s.toCharArray and the index i to access the right element */    
                char c = reader.next().charAt(0);
                /* Fill the 2-dimentional array of character with c at x and y coordinates */    
                characters [x][y] = c;
                /* Update the x coordinate */    
                x += 1;   }   
                /* Reset the x coordinate and update the y coordinate */   
                x = 0;   
                y += 1;  } 

        reader.close();  
        return characters; }   /* Close the scanner and return the 2-dimensional array */ 

}

【问题讨论】:

    标签: java methods parameters parameter-passing


    【解决方案1】:

    您必须指定变量的类型。第 22 行和第 88 行,修复了所有错误。

    在定义参数&lt;identifier&gt; &lt;variable_name&gt;时遵循此模式

    【讨论】:

    • 我已经在 main 方法中指定了变量的类型。当我将它们作为参数传递时,我是否必须再次指定它们的类型?认为如果我这样做会创建第二个同名变量。
    • @Charlie 您必须在定义参数时指定类型。而且您正在处理不同的范围,它们可以具有相同的名称。这里的问题是缺少数据类型。读一读w3schools.com/java/java_methods_param.asp
    • 好的,我现在已经在参数中指定了数据类型,如下所示: public static BufferedImage imageFromText (int image_width, int image_height, String input_file_path, String output_file_path, int r, int g, int b, char [][] 个字符)抛出 IOException{
    • 现在我被告知 main 方法中的变量从未使用过。我在方法中放了一些打印语句来测试变量是否输出任何东西并且它们是空白的。
    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 2018-03-09
    • 2021-10-30
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多