Java 尝试将变量用作参数时出现各种错误

Java 尝试将变量用作参数时出现各种错误,java,methods,parameters,parameter-passing,Java,Methods,Parameters,Parameter Passing,我正在编写一个程序来输入一个文本文件,其中包含一个50×50的块,由三个不同的字符组成:#、*和/,并根据每个符号给输出PNG文件的每个像素上色,这样每个字符都有一个颜色。但是,我遇到了一些与将变量作为参数传递有关的语法错误。以下列出了这些语法错误: Syntax error on token ",", < expected [21 , 59] Syntax error, insert "Dimensions" to complete ArrayType [22,5] Syntax err

我正在编写一个程序来输入一个文本文件,其中包含一个50×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]
令牌“,”上的语法错误,<预期为[21,59] 语法错误,插入“维度”以完成ArrayType[22,5] 语法错误,插入“>”以完成引用类型1[22,5] 标记“,”上的语法错误,删除此标记[88,53] 语法错误,插入“…VariableDeclaratorId”以完成FormalParameter[88,69] 图像宽度无法解析为变量[90,40] 图像_高度无法解析为变量[90,53] 无法将输入文件路径解析为变量[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 */ 

}
import java.util.Scanner;
导入javax.imageio.imageio;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.PrintWriter;
公共课实验室8周ImgFrmTxt第2部分{
公共静态void main(字符串[]args)引发异常{
int图像_宽度=50;
int图像_高度=50;
字符串输出文件路径=(“image.png”);
字符串输入文件\u path=“superimage.txt”;
}
公共静态缓冲区imageFromText(图像宽度、图像高度、输入文件路径、输出文件路径、,
int r、int g、int b、char[][]个字符)引发IOException{
如果(图像宽度255 | | b<0 | | b>255){
System.err.println(“颜色值(r、g、b)必须介于0和255之间!”);
返回;
}
int a=255;//α值
/*
*写入像素的不同值,即红色、绿色、蓝色和alpha。
*不同的颜色值都使用字节运算符存储到单个整数中。

*a=x必须指定变量的类型。第22行和第88行修复所有错误


定义参数时遵循此模式

我在main方法中指定了变量的类型。然后,当我将它们作为参数传递时,是否必须再次指定它们的类型?如果这样做,会创建第二个同名变量。@Charlie在定义参数时必须指定类型。您在不同的作用域上工作,它们可以具有相同的名称。这里的问题是缺少数据类型。请对此进行读取确定,我现在在参数中指定了数据类型,如:public static BufferedImage fromtext(int image_width,int image_height,String input_file_path,String output_file_path,int r,int g,int b,char[][]字符)抛出IOException{现在我被告知main方法中的变量从未被使用过。我在方法中放了一些print语句来测试变量是否输出任何内容,它们是否为空。