[英]Java: System.out.println() should write to the console, but it doesn't
[英]System.out.println doesn't show text in console (IntelliJ)
我正在编写一个程序,其部分如下所示:
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();
}
}
代码分为两个 Java .class 文件。
我正在处理的问题是使用p1.setPortal();
不会在 IntelliJ 控制台中显示文本。 该程序虽然工作并返回 0。当我将System.out.println
放在 main 以外的方法中(也在单独的类文件中)时,我在另一个程序中没有这样的问题。 造成此类问题的原因可能是什么?
它应该正确抛出异常,因为您忘记初始化整数数组。 看看这个线程: Do we need to initialize an array in Java?
对于整数数组,Java 的默认值为 null。 所以你的 for 甚至不会循环。 唯一让我感到奇怪的是为什么没有例外..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.