gpt4 book ai didi

java - 多个 "cannot find symbol"错误

转载 作者:行者123 更新时间:2023-12-01 11:09:43 25 4
gpt4 key购买 nike

我正在解决一个中间 Java 类的家庭作业问题,但我找不到错误的根源。我收到 50 多个关于宽度、高度、网格和种子的“找不到符号”错误。之前的构造函数存在问题(我在 if 语句中声明了变量),我认为这可能就是问题所在,但我已经修补并用 Google 搜索过,但无法确定问题所在。 (我正在终端中工作,而不是 IDE。)

//single particle diffusion
import java.util.Random;
import java.io.PrintStream;

/*****************************/
/*****************************/
/* Start Public Class NFrostN /
/*****************************/
/*****************************/

public class NFrostN {

/*****************************/
/*****************************/
/*********Constructor*********/
/*****************************/
/*****************************/
public NFrostN(String[] args){
int width;
int height;
int density;
int seed;
if (args.length > 0){
//makes sure no more than 4 have been entered
if (!this.checkArgs(args))
{
System.err.print("There's a problem with the arguments. Make sure you haven't entered a string or more than four arguments.");
System.exit(1);} //exit with non-0 status
this.setWidth(args);
this.setHeight(args);
this.setDensity(args);
this.setSeed(args);
}
int[][] grid = new int[this.width][this.height];
}


public NFrostN(){
int width;
int height;
int density;
int seed;
this.setWidth();
this.setHeight();
this.setDensity();
this.setSeed();
int[][] grid = new int[this.width][this.height];
}




/*****************************/
/*****************************/
/***Create a Random Integer***/
/******Regular+overloaded*****/
/*****************************/

int createRandomInt(int seed,int size){
int randomb = (int) this.randoma.nextInt(size);
return randomb;
}

int createRandomInt(){
int randomb = (int) randoma.nextInt();
// int randomc = (int) randomb.nextInt(randomb);
return randomb;
}

/*****************************/
/*****************************/
/**Verify arguments and Set***/
/*****************************/
/*****************************/

private boolean checkArgs(String[] args) {
if(args.length > 4) //if too many arguments entered. not specified, made the call
{return false;}
else return true;
}
/*****************************/
/*****************************/
/**Width Setter+ Overloaded***/
/*****************************/
/*****************************/

void setWidth(String args[]){
int tempWidth;
if (args.length > 0){
int tempWidth = Integer.parseInt(args[0]);
if(tempWidth % 1 == 0 || tempWidth > 0)
{
this.width = tempWidth;
}
//error message??
}
else
width = 256;
}

void setWidth(){width = 256;}


/*****************************/
/*****************************/
/**Height Setter+ Overloaded**/
/*****************************/
/*****************************/

void setHeight(String args[]){
if (args.length > 1 ){
int tempHeight = Integer.parseInt(args[1]);
if(tempHeight % 1 == 0 || tempHeight > 0)
{
height = tempHeight;
}
//error message??
}
else height = 256;
}

void setHeight(){height = 256;}


/*****************************/
/*****************************/
/*Density Setter+ Overloaded**/
/*****************************/
/*****************************/

void setDensity(String args[]){
if (args.length > 2){
int tempDensity = Integer.parseInt(args[2]);
if(tempDensity % 1 == 0 || tempDensity > 0 || tempDensity <= 100)
{
density = tempDensity;
}
//error message???
}
else density = 25;
}


void setDensity(){density = 25;}


/*****************************/
/*****************************/
/***Seed Setter+ Overloaded***/
/*****************************/
/*****************************/

void setSeed(String args[]){
int tempSeed = Integer.parseInt(args[3]);
int myRandom = (int) this.randoma.nextInt();
if (args.length > 3){
if(tempSeed % 1 == 0 || tempSeed > 0 || tempSeed <= 100)
{
this.seed = tempSeed;
}
//error message ?
}
else seed = myRandom;
}


Random makeRand(){
Random randoma = new Random(seed);
return randoma;
}

void setSeed(){
Random newRand = makeRand;
int myRandom = (int) newRand.nextInt();
seed = myRandom;
}

/*****************************/
/*****************************/
/**********Getters************/
/*****************************/
/*****************************/

int getWidth(){return width;}
int getHeight(){return height;}
int getDensity(){return density;}
int getSeed(){return seed;}
int[][] getGrid(){return grid;}



/*****************************/
/*****************************/
/*****Sum of all in array*****/
int gridSum(){
int count = 0;
for (int i = 0; i < height; i++)
for (int j = 0; i < width; i++){
{count += grid[height][width];}
}
return count;
}




/*****************************/
/*****************************/
/*******Difusion Method*******/
/*****************************/
/*****************************/


boolean diffuse(){
boolean filled = false;
int diffusionSteps = ((this.height * this.width) * 2); //two times the size of the grid
int steps = 0; //steps counter
Random genRandom = new Random(seed);
int indexA = (int) genRandom.nextInt(width); //indexes within domain and range of grid
int indexB = (int) genRandom.nextInt(height);

while (steps <= diffusionSteps){
NFrostN r = new NFrostN();
// int indexA = (int) r.createRandomInt(seed,size);
// int indexB = (int) r.createRandomInt(indexA,size);
if (grid[indexA][indexB] == 0) { //if empty
if (grid[indexA-1][indexB] != 0 || grid[indexA][indexB-1] != 0 || grid[indexA+1][indexB] != 0 || grid[indexA][indexB+1] != 0) //if sticks next to a particle...
{grid[indexA][indexB] = 1;
return true;
}
else {
steps += 1; //increase counter before moving and looping again
Random genRandomnew = new Random(seed);
int ofFour = genRandomnew.nextInt(4);
switch(ofFour)
{
case 1:
indexA++;
return true;


case 2:
indexB++;
return true;

case 3:
indexB++;
return true;

case 4:
indexA++;
return true;

}
}
}

}
}

/*****************************/
/*****************************/
/********Display Method*******/
/*****************************/
/*****************************/

private void display(){
PrintStream ps = new PrintStream(OutputStream);
System.out.println("P6"); //magic number
System.out.println(width); //which is which??
System.out.println(height); //which is which??
System.out.println(110); //max value
for (int i = 0; i < width; i++){
for (int k = 0; k < this.height; k++){
if (this.grid[this.width][this.height] == 1){
byte[] rgb = {110,110,110};
ps.write(rgb);
}
else
{byte[] rgb = {0,0,0};
ps.write(rgb);
}
}

}
ps.close();
}



/*****************************/
/*****************************/
/*************Main************/
/*****************************/
/*****************************/


public static void main(String[] args) {
NFrostN frost = new NFrostN(args);
int currentFull = frost.gridSum();
while ((currentFull/(frost.getHeight()*frost.getWidth()))*100 <= frost.getDensity())
{ //while the percentage filled is smaller or equal to the density, diffuse
if (frost.diffuse() == true)
{
System.exit(0);
}
if (frost.diffuse() == false)
{
System.err.print("Error in method difuse");
System.exit(1);
}
frost.display();
currentFull = frost.gridSum();
}
}

}

最佳答案

您应该将这些字段声明为实例变量。但您已将它们声明为 block/local 变量,因此您无法通过 this.width 访问。此外,当您在构造函数内部声明变量时,您无法在构造函数外部访问它们。

像这样

public class NFrostN {

int width;
int height;
int density;
int seed;

当您调用 this.width 编译器时,会看到没有实例/width 变量,并给出找不到符号错误

关于java - 多个 "cannot find symbol"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32515562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com