简体   繁体   English

使用 inheritance 和 NetBeans 运行程序,因为 NetBeans output 不应该“工作”

[英]Run a program using inheritance with NetBeans because NetBeans output doesn't “work” as it should

Yeah, the title isn't very descriptive but that is because I don't know how call this problem.是的,标题不是很有描述性,但那是因为我不知道如何称呼这个问题。

The problem is the next: I use inheritance with a list of a steps, for example a sum:问题是下一个:我使用 inheritance 和一个步骤列表,例如总和:

  1. Message of "give me the first number" “给我第一个号码”的消息
  2. "Insert first number" “插入第一个数字”
  3. Message of "give me the second number" “给我第二个号码”的消息
  4. "Insert second number" “插入第二个数字”
  5. Show the result显示结果

But the output make this:但是 output 做到了:

  1. "Insert first number" 2 "Insert second number" "插入第一个数字" 2 "插入第二个数字"
  2. Message of "give me the first number" “给我第一个号码”的消息
  3. Message of "give me the second number" “给我第二个号码”的消息
  4. Show the result (but ignoring the existance of the first number)显示结果(但忽略第一个数字的存在)

Now this is the code Code of JavaClassPrueba1A:现在这是JavaClassPrueba1A的代码代码:

package package1;
import java.util.Scanner;

public class JavaClass1A {
    protected int value1, value2, result;
    Scanner dataEntry = new Scanner(System.in);
    
    //Este método pide los valores al usuario
    public void RequestData(){
        System.out.print("Give me the first value: ");
        value1 = dataEntry.nextInt();
        System.out.print("Give me the second value: ");
        value2 = dataEntry.nextInt();
    }
    
    //Este método muestra el resultado
    public void ShowResult(){
        System.out.println(result);
    }
}

Code of JavaClassPrueba2A: JavaClassPrueba2A的代码:

package package1;
public class JavaClass2A extends JavaClass1A{
    public void Sum(){
        result = value1 + value2;
    }
}

Code of JavaClassPrueba3A: JavaClassPrueba3A的代码:

package package1;

public class JavaClass3A extends JavaClass1A{
    public void Subtraction(){
        result = value1 - value2;
    }
}

Code of MainClass1A (this is the class that run all): MainClass1A 的代码(这是运行所有的 class):

package MetodoMain;
import package1.JavaClass2A;
import package1.JavaClass3A;

public class MainClass1A {
    public static void main(String[] args){
        
        JavaClass2A messageSum = new JavaClass2A();
        messageSum.RequestData();
        messageSum.Sum();
        System.out.print("The resultado of the sum is: ");
        messageSum.ShowResult();
        
        JavaClass3A messageSubtraction = new JavaClass3A();
        messageSubtraction.RequestData();
        messageSubtraction.Subtraction();
        System.out.print("The resultado of the Subtraction is: ");
        messageSubtraction.ShowResult();
    }
}

And this is a copy of all run.这是所有运行的副本。 The problem here is a problem of in the order in which things are displayed/run (for this reason don't exist error message [yes i am very redundant])这里的问题是显示/运行顺序的问题(因此不存在错误消息[是的,我非常多余])

cd C:\Users\Usuario\Documents\NetBeansProjects\JavaClassPrueba2; cd C:\Users\Usuario\Documents\NetBeansProjects\JavaClassPrueba2; "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_151" cmd /c ""C:\Program Files\NetBeans-12.1\netbeans\java\maven\bin\mvn.cmd" -Dexec.args="-classpath %classpath MetodoMain.MainClass1A" -Dexec.executable="C:\Program Files\Java\jdk1.8.0_151\bin\java.exe" -Dexec.classpathScope=runtime -Dmaven.ext.class.path="C:\Program Files\NetBeans-12.1\netbeans\java\maven-nblib\netbeans-eventspy.jar" -Dfile.encoding=UTF-8 org.codehaus.mojo:exec-maven-plugin:1.5.0:exec" Running NetBeans Compile On Save execution. "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_151" cmd /c ""C:\Program Files\NetBeans-12.1\netbeans\java\maven\bin\mvn.cmd" -Dexec.args="-类路径 %classpath MetodoMain.MainClass1A" -Dexec.executable="C:\Program Files\Java\jdk1.8.0_151\bin\java.exe" -Dexec.classpathScope=runtime -Dmaven.ext.class.path="C: \Program Files\NetBeans-12.1\netbeans\java\maven-nblib\netbeans-eventspy.jar" -Dfile.encoding=UTF-8 org.codehaus.mojo:exec-maven-plugin:1.5.0:exec" 运行 NetBeans保存执行时编译。 Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.跳过阶段执行,将使用依赖项目的 output 目录(打开保存时编译)而不是它们的 jar 工件。

Scanning for projects...正在扫描项目...

------------------------< DOS:JavaClassPrueba2 >------------------------ ------------< DOS:JavaClassPrueba2 >--------- ---

Building JavaClassPrueba2 1.0.0-SNAPSHOT构建 JavaClassPrueba2 1.0.0-SNAPSHOT

--------------------------------[ jar ]--------------------------------- --------------------------------[ jar ]------------- ------------------

--- exec-maven-plugin:1.5.0:exec (default-cli) @ JavaClassPrueba2 --- --- exec-maven-plugin:1.5.0:exec (default-cli) @ JavaClassPrueba2 ---

5 this is a number that I inputed like the other 3 5这是我像其他 3 一样输入的数字

10 10

Give me the first value: Give me the second value: The resultado of the sum is: 15给我第一个值:给我第二个值:总和的结果是:15

90 90

100 100

Give me the first value: Give me the second value: The resultado of the Subtraction is: -10给我第一个值:给我第二个值:减法的结果是:-10


BUILD SUCCESS建立成功

Total time: 01:05 min Finished at: 2021-01-20T16:00:24-03:00总时间:01:05 分钟完成时间:2021-01-20T16:00:24-03:00

And this is a screenshot of the same message of the run这是运行相同消息的屏幕截图

It is a bug of maven on NetBeans.这是 NetBeans 上的 maven 的错误。 Try to change尝试改变

    System.out.print()

to

    System.out.println() 

in method RequestData()在方法RequestData()

It looks to me as if NetBeans is buffering the output, and may be waiting for an end-of-line character before it writes the output it has collected so far to the Output window. It looks to me as if NetBeans is buffering the output, and may be waiting for an end-of-line character before it writes the output it has collected so far to the Output window. You are using System.out.print , and this doesn't write out an end-of-line character after writing the text.您正在使用System.out.print ,这不会在写入文本后写出行尾字符。

Try adding the line尝试添加行

System.out.flush();

after both calls to System.out.print() in JavaClass1A.RequestData() .JavaClass1A.RequestData()中两次调用System.out.print()之后。 By calling flush() , this might force the NetBeans Output window to display the output it has received so far without waiting for an end-of-line character. By calling flush() , this might force the NetBeans Output window to display the output it has received so far without waiting for an end-of-line character.

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

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