简体   繁体   English

System.out.println 不在控制台中显示文本(IntelliJ)

[英]System.out.println doesn't show text in console (IntelliJ)

I am writing a program which part is presented below:我正在编写一个程序,其部分如下所示:

public class Portal {

    private String name;
    private int[] positions;              // positions of "ship"
    private static int moves = 0;         // moves made by player to sink a ship
    public static int shot;               // the value of position given by player
    private int hits = 0;                 // number of hits
    private int maxSize = 4;              // max size of ship (the size will be randomized)
    int first;                            // position of 1st ship block
    int size;                             // real size of ship (randomized in setPortal method)

    public void checkIfHit(){
        for (int i : positions){
            if (i == shot){
                System.out.println("Hit confirmed");
                hits++;
            } else if (hits == positions.length){
                System.out.println("Sunk");
            } else {
                System.out.println("Missed it");
            }
        }
        moves++;
    }

    public void setPortal(){
        size = 1 + (int)Math.random()*maxSize;
        for (int i = 0; i < size - 1; i++){
            if (i == 0){
                positions[i]= 1 + (int)Math.random()*positions.length;
                first = positions[i];
                System.out.println(positions[i]);
                continue;
            }
            positions[i]= first + 1;
            System.out.println(positions[i]);

        }
    }
}

public class Main{

    public static void main(String[] args){
        // write your code here
        Portal p1 = new Portal();
        p1.setPortal();
    }
}

code is split in two Java .class files.代码分为两个 Java .class 文件。

The problem I'm dealing with is that using p1.setPortal();我正在处理的问题是使用p1.setPortal(); doesn't show up text in IntelliJ console.不会在 IntelliJ 控制台中显示文本。 The program works though and returns 0. I don't have such problem in another program when I've put System.out.println in method other than main (also in separate class file).该程序虽然工作并返回 0。当我将System.out.println放在 main 以外的方法中(也在单独的类文件中)时,我在另一个程序中没有这样的问题。 What may be the cause of such issue?造成此类问题的原因可能是什么?

It should properly throw an exception, because you forgot to initialize the integer array.它应该正确抛出异常,因为您忘记初始化整数数组。 Have a look at this thread: Do we need to initialize an array in Java?看看这个线程: Do we need to initialize an array in Java?

The Java's default value is null for an integer array.对于整数数组,Java 的默认值为 null。 So your for wont even loop trough.所以你的 for 甚至不会循环。 The only thing that wonders me is why there is no exception..唯一让我感到奇怪的是为什么没有例外..

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

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