我正在处理中级Java课程的家庭作业问题,但我无法找到我遇到的错误的来源。我得到50+"找不到符号"宽度,高度,网格和种子的错误。之前的构造函数存在问题(我在if语句中声明了变量),我认为这可能是问题的所在,但是我已经修改了和谷歌搜索,我无法确定问题。 (我在终端工作,而不是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();
}
}
}
答案 0 :(得分:0)
您应该直接在类下声明变量,而不是构造函数定义的一部分。使用构造函数将值赋给成员变量
公共类NFrostN {
int width;
int height;
int density;
int seed;
public NFrostN(String[] args){
#use the constructor to initialize the variables
}
}