【问题标题】:Saving value in boolean statement Java在布尔语句 Java 中保存值
【发布时间】:2015-09-21 13:20:27
【问题描述】:

编辑

我有输入 5 3,其中 10 是每行的字符数量,5 (StartA) 是该行中 * 的位置。

我需要将此输入保存在布尔数组中,以便保存 {假,假,真,假,假} 目前,程序在执行时会给出一个 nullPointerExeption: CurrentState[i] == true

请您解释一下我是如何编程的。

    class Program{
  Scanner sc = new Scanner(System.in);

  int L; // #characters per line
  int G; // # lines
  int Start; // Starting position
  int i; // position of character calculated
  int k; // Position in new line
  int j; // Row working
  String choice; // choice which program to execute
  boolean firstLine[] = new boolean[L+1];
  boolean nextGen[] = new boolean[L+1];

  void input() {
    L = sc.nextInt(); // # Characters per line
    G = sc.nextInt(); // # lines
    Start = sc.nextInt(); // Characters which is a * in first line
  }
  // output for first line
  void line() { 
    for (i=0; i<L+1; i++) {
      if (i == Start-1) {
        CurrentState[i] = true;
        System.out.print("*");
      } else if (i==L) {
        System.out.println("");
      } else {
        CurrentState[i] = false;
        System.out.print(".");
      }
    }
  }
  // rules for calculating the next line
  void rules() {
    for ( k=0; k<L-1; k++) {
      for ( i=1; i<L-1; i++) {
        if (CurrentState[i-1] == true && CurrentState[i]== true && CurrentState[i+1] == true) {
          nextGen[k] = false;
        } else if (CurrentState[i-1] == true && CurrentState[i] == true && CurrentState[i+1] == false) {
          nextGen[k] = true;
        } else if (CurrentState[i-1] == false && CurrentState[i] == true && CurrentState[i+1] == true) {
          nextGen[k] = true;
        } else if (CurrentState[i-1] == false && CurrentState[i] == false && CurrentState[i+1] == false) {
          nextGen[k] = false;
        } else if (CurrentState[i-1] == true && CurrentState[i] == false && CurrentState[i+1] == true) {
          nextGen[k] = true;
        } else if (CurrentState[i-1] == false && CurrentState[i] == false && CurrentState[i+1] == true) {
          nextGen[k] = true;
        } else if (CurrentState[i-1] == true && CurrentState[i] == false && CurrentState[i+1] == false) {
          nextGen[k] = true;
        } else if (CurrentState[i-1]== false && CurrentState[i] == true && CurrentState[i+1]== false) {
          nextGen[k] = false;
        }  
      }
      return true;
    }
  }
  // gives output for line 2 till G.
  void nextLine() {     
    for (j=0; j<G-1; j++){
      for (k=0; k<L; k++){
        if (nextGen[k] == true) {
          System.out.print("*");
        } else if (k==L-1) {
          System.out.println("");
        } else {
          System.out.print(".");
        }
      }
    } 
  }
  void run() {
    input();
    line();
    rules();
    nextLine();
  }

  public static void main(String[] args) {
    new Program().run();
  }
}

【问题讨论】:

  • 你初始化 CurrentState 了吗?您需要像这样声明数组,然后才能使用它:boolean CurrentState[] = new boolean[x],其中x 是您想要的元素数。
  • 好的。我需要把这条线放在哪里?
  • @Peter 在类的构造函数或属性声明中。

标签: java boolean


【解决方案1】:

你得到一个空指针异常,因为布尔数组没有初始化。您需要执行以下操作:

boolean CurrentState[] = new boolean[L+1];

编辑:下面的程序有效。我尝试使用 8、7 和 4 作为输入,并将其作为输出: ...*.... ………… ………… ………… ………… ………… .......

import java.util.Scanner;

class Program{
    Scanner sc = new Scanner(System.in);

    int L; // #characters per line
    int G; // # lines
    int Start; // Starting position
    int i; // position of character calculated
    int k; // Position in new line
    int j; // Row working
    String choice; // choice which program to execute
    boolean firstLine[];
    boolean nextGen[];
    boolean CurrentState[];

    void input() {
        L = sc.nextInt(); // # Characters per line
        G = sc.nextInt(); // # lines
        Start = sc.nextInt(); // Characters which is a * in first line
        CurrentState = new boolean[L+1];
        firstLine = new boolean[L+1];
        nextGen = new boolean[L+1];
    }
    // output for first line
    void line() {

        for (i=0; i<L+1; i++) {
            if (i == Start-1) {
                CurrentState[i] = true;
                System.out.print("*");
            } else if (i==L) {
                System.out.println("");
            } else {
                CurrentState[i] = false;
                System.out.print(".");
            }
        }
    }
    // rules for calculating the next line
    void rules() {
        for ( k=0; k<L-1; k++) {
            for ( i=1; i<L-1; i++) {
                if (CurrentState[i-1] == true && CurrentState[i]== true && CurrentState[i+1] == true) {
                    nextGen[k] = false;
                } else if (CurrentState[i-1] == true && CurrentState[i] == true && CurrentState[i+1] == false) {
                    nextGen[k] = true;
                } else if (CurrentState[i-1] == false && CurrentState[i] == true && CurrentState[i+1] == true) {
                    nextGen[k] = true;
                } else if (CurrentState[i-1] == false && CurrentState[i] == false && CurrentState[i+1] == false) {
                    nextGen[k] = false;
                } else if (CurrentState[i-1] == true && CurrentState[i] == false && CurrentState[i+1] == true) {
                    nextGen[k] = true;
                } else if (CurrentState[i-1] == false && CurrentState[i] == false && CurrentState[i+1] == true) {
                    nextGen[k] = true;
                } else if (CurrentState[i-1] == true && CurrentState[i] == false && CurrentState[i+1] == false) {
                    nextGen[k] = true;
                } else if (CurrentState[i-1]== false && CurrentState[i] == true && CurrentState[i+1]== false) {
                    nextGen[k] = false;
                }
            }
        }
    }
    // gives output for line 2 till G.
    void nextLine() {
        for (j=0; j<G-1; j++){
            for (k=0; k<L; k++){
                if (nextGen[k] == true) {
                    System.out.print("*");
                } else if (k==L-1) {
                    System.out.println("");
                } else {
                    System.out.print(".");
                }
            }
        }
    }
    void run() {
        input();
        line();
        rules();
        nextLine();
    }

    public static void main(String[] args) {
        new Program().run();
    }
}

【讨论】:

  • 好的。我需要把这条线放在哪里?
  • 在方法的顶部就可以了。
  • 当我这样做时,我立即得到一个 NullPointerExeption。
  • 您是否在与以前相同的位置获得异常?你能把你放置数组声明的地方发到这里吗?
  • 在早期阶段没有。在添加这一行之前,输出是 prgram 的第一行。 boolean firstline[] = new boolean[L+1]; for (i=0; i
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-03
  • 2015-05-18
  • 2013-03-01
  • 2015-02-22
  • 2018-04-17
  • 2014-08-19
相关资源
最近更新 更多