繁体   English   中英

Java-使用单独的方法计算随机生成器方法中字符的出现

[英]Java - counting the occurrences of a character in a random generator method using a separate method

我试图弄清楚如何计算上次使用print()方法时打印的星星数量。

我对如何将starsInLastPrint变量的值带入starsInLastPrint()方法感到困惑。 我的理解是这是不可能的。 我认为当前的代码有很多问题没有帮助。 以下是我被困时的当前状态。

import java.util.Random;

public class NightSky {

private double density;
private int width;
private int height;
private int starsInLastPrint;

public NightSky(double density) {
    width = 20;
    height = 10;
    this.density = density;
}

public NightSky(int width, int height) {
    density = 0.1;
    this.width = width;
    this.height = height;

}

public NightSky(double density, int width, int height) {
    this.density = density;
    this.width = width;
    this.height = height;
}

public void printLine() {
    Random starPlacement = new Random();
    String[] stars = new String[(this.width)];

    for (int i = 0; i < this.width; i++) {
        double random = starPlacement.nextDouble();
        if (random <= this.density) {
            stars[i] = "*";
            this.starsInLastPrint++;
        } else {
            stars[i] = " ";
        }
    }

    int j = 0;
    while (j < stars.length) {
        System.out.print(stars[j]);
        j++;

    }
    System.out.println("");

}

public void print() {

    NightSky nightSky = new NightSky(this.density, this.width, this.height);

    this.starsInLastPrint = 0;
    int i = 0;

    while (i < this.height) {
        nightSky.printLine();
        i++;
    }

}

public int starsInLastPrint() {

    return this.starsInLastPrint;

}

}

您走在正确的轨道上。 但是,您无需在print方法中实例化另一个NightSky对象。 您可以执行以下操作,

public void print() {
    this.starsInLastPrint = 0;
    int i=0;
    while (i < this.height) {
        printLine();
        i++;
    }

}

因此,每次调用print时,它将更新该print方法调用的星数。 这是整个代码,

import java.util.Random;

public class NightSky {

private double density;
private int width;
private int height;
private int starsInLastPrint;

public static void main(String[] args){

    NightSky sky = new NightSky(5,5);
    sky.print();
    System.out.println(sky.starsInLastPrint());

    sky.print();
    System.out.println(sky.starsInLastPrint());
}


public NightSky(double density) {
    width = 20;
    height = 10;
    this.density = density;
}

public NightSky(int width, int height) {
    density = 0.1;
    this.width = width;
    this.height = height;

}

public NightSky(double density, int width, int height) {
    this.density = density;
    this.width = width;
    this.height = height;
}

public void printLine() {
    Random starPlacement = new Random();
    String[] stars = new String[(this.width)];

    for (int i = 0; i < this.width; i++) {
        double random = starPlacement.nextDouble();
        if (random <= this.density) {
            stars[i] = "*";
            this.starsInLastPrint++;
        } else {
            stars[i] = " ";
        }
    }

    int j = 0;
    while (j < stars.length) {
        System.out.print(stars[j]);
        j++;

    }
    System.out.println("");

}

public void print() {
    this.starsInLastPrint = 0;
    int i=0;
    while (i < this.height) {
        printLine();
        i++;
    }

}

public int starsInLastPrint() {

    return this.starsInLastPrint;

}

}

上面的代码示例运行:

*   *
  *  

  *  
4





0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM