gpt4 book ai didi

Java 标准输出不产生输出

转载 作者:行者123 更新时间:2023-12-01 23:42:13 25 4
gpt4 key购买 nike

我遇到了一些麻烦。此代码不会引发任何编译时错误,但 System.out.println 未显示。它应该显示您应该如何为所有四个侧面着色。我在 for 循环中犯了错误还是结构错误?

参见下面的代码:

public interface Colorable {
// Abstract to be called later
public abstract String howToColor();

}

public class HowToColour {

/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
}

public class TestColorable {
public static void main(String[] args) {
Object[] obj1 = {
new Square(),
new Rectangle(),
new Rhombus(),
new Parallelogram(),
new Trapezium()
};
for (int i = 0; i < obj1.length; i++) {
if (obj1[i] instanceof Colorable) {
System.out.println(((Colorable) obj1[i]).howToColor());
} else {
System.out.println("This shape is not to be colored");

}
}
}
}

class GeometricOgject {

}

// Initial Method to use the interface
class Square extends GeometricOgject implements Colorable {
@Override
public String howToColor() {
return "Square: Color all four sides";
}
}

// Method to use an interface in an abstract class
abstract class FourSides implements Colorable {

}

class Rectangle extends FourSides {
@Override
public String howToColor() {
return "Rectangle: color all four sides";
}
}

class Rhombus extends FourSides {
@Override
public String howToColor() {
return "Rhombus: color all four sides";
}
}

class Parallelogram extends FourSides {
@Override
public String howToColor() {
return "Parallelogram: color all for sides";
}
}

class Trapezium extends FourSides {
@Override
public String howToColor() {
return "Trapezium: color all four sides";
}
}

最佳答案

如果这一切都在一个文件中,我可以看到与具有 main 函数的两个类的声明存在潜在冲突。巧合的是,您的一根电源是空的,而这可能就是正在执行的电源。如果这些位于不同的文件中,您应该能够在命令行上执行此操作:

java TestColorable

我认为正在发生的事情相当于:

java HowToColour

执行以下操作,每项都在其自己的文件中:

public class HowToColour  
{
//implementation
}

public class TestColorable
{
//implementation
}

javac TestColorable.java
javac HowToColour.java

java TestColorable
**output will go here**
java HowToColour
**no output here**

关于Java 标准输出不产生输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17726352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com