eclipse Java程序在eclipse中终止而不运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25907081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:59:47  来源:igfitidea点击:

Java Program terminated in eclipse without running

javaeclipse

提问by user3483844

I'm new to Java and I'm just trying to run a simple program using eclipse that takes numbers that are factors of 3 or 5 through 0 to 1000 and adds them all together. I try to run the code, but the program just terminates after a second of running and displays nothing on my console. Here's my code.

我是 Java 新手,我只是想使用 eclipse 运行一个简单的程序,该程序采用 3 或 5 到 0 到 1000 的因数并将它们加在一起。我尝试运行代码,但程序在运行一秒钟后终止,并且在我的控制台上不显示任何内容。这是我的代码。

public class PrimeSum {


    public static void main(String args[]){

    }
    private double Num= 0.0;
    private double sum = 0.0;{

    for(int i=0;i<1001;i++){
        Num = i;
        if(i % 3 == 0.0){
            sum += i;
        if(i % 5 == 0.0){
            if(i % 3 != 0.0){
                sum += i;
            }
        }
        }

    }

    System.out.println("The total is "+ sum);
}

Can someone tell me why this is, I've been searching for these past 2 hours and come up with nothing.

有人能告诉我这是为什么吗,过去 2 个小时我一直在寻找,但一无所获。

回答by Juned Ahsan

Your main method is empty. So nothing happens:

您的主要方法是空的。所以什么也没有发生:

public static void main(String args[]){

}

Probably you want to create a method but you just created a code block here:

可能您想创建一个方法,但您刚刚在此处创建了一个代码块:

private double sum = 0.0;{

    for(int i=0;i<1001;i++){
        Num = i;
        if(i % 3 == 0.0){
            sum += i;
        if(i % 5 == 0.0){
            if(i % 3 != 0.0){
                sum += i;
            }
        }
        }

    }

Now even this code may work once you create an object of your class in main method. Because this code block will execute on object creation.

现在,一旦您在 main 方法中创建了类的对象,即使此代码也可以工作。因为此代码块将在对象创建时执行。

I can't explain all the basics in this answer about code structure. But this is maybe what you want:

我无法解释此答案中有关代码结构的所有基础知识。但这也许是你想要的:

public class PrimeSum {

    public static void main(String args[]){
    PrimeSum obj =  new PrimeSum(); // creating an instance of your class will trigger the instance code block
    }
    private double Num= 0.0;
    private double sum = 0.0;{

    for(int i=0;i<1001;i++){
        Num = i;
        if(i % 3 == 0.0){
            sum += i;
        if(i % 5 == 0.0){
            if(i % 3 != 0.0){
                sum += i;
            }
        }
        }
    }   
    System.out.println("The total is "+ sum);
    }
}

回答by user2864740

Nothing is displayed because the looping code and println doesn't run. The construct used is an instance initialization block. However, an instance of the PrimeSum class is never created- hence the block never executes.

没有显示任何内容,因为循环代码和 println没有运行。使用的构造是实例初始化块。但是,从未创建PrimeSum 类的实例- 因此块永远不会执行

The simple fix is to move the code into the main method which isexecuted. (Note that it is staticso it can be called withoutan instance being created.)

简单的解决方法是在代码移动到其中的主要方法执行。(请注意,它static可以在创建实例的情况下调用。)

Consider:

考虑:

public class PrimeSum {

    public static void main(String args[]){
        System.out.println("Hi, in main!");
        // Now create instance, run initialization block..
        new PrimeSum();
        // .. but really, just put the code in main, or better,
        // a method called from main ..
        System.out.println("Sum is " + calculateSum());
    }

    /* private double sum = 0.0; <-- note newlines added here for clarity */

    {
       // This is an instance initialization block, it does NOT run
       // until/when an instance is created.
       // (The original never ran code in here, because an instance was
       //  never created.)
       System.out.println("Hi, in instance initialization block!");
    }

    static double calculateSum() {
        // Do math, return value
        return 42;
    }
}

回答by codebot

You don't have any coding in your mainmethod. So I think you are expecting for a visible output on console. If you need to see your result in a console, You should add a System.out.println()to your code.

您的main方法中没有任何编码。所以我认为你期待在控制台上有一个可见的输出。如果您需要在控制台中查看结果,您应该System.out.println()在代码中添加一个。

回答by user7146343

import java.sql.*;
public class JDBCConnect 
{

public static void main(String[] args) {

Connection con;
Statement st;
ResultSet rs;
int no;
String nm,typ;
double bal;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@217.212.7.169:1521:cashutv3","cash_test","cash_test");
st=con.createStatement();
rs=st.executeQuery("select * from accounts");

while(rs.next())
{
no=rs.getInt("accno");
nm=rs.getString("accnm");
typ=rs.getString("acctype");
bal=rs.getDouble("balance");
System.out.println("account no is "+no);
System.out.println("Name is "+nm);
System.out.println("account type is "+typ);
System.out.println("balance is "+bal);
}
con.close();
}

catch(Exception e)
{
System.out.println(e);
}

}


    }

// program got terminated and its displaying the path of javaw.exe file

回答by ARIJIT

Here is the modified code :

这是修改后的代码:

public class PrimeSum {
  public static void main(String args[]) {

        double Num = 0.0;
        double sum = 0.0;
        {

            for (int i = 0; i < 1001; i++) {
                Num = i;
                if (i % 3 == 0.0) {
                    sum += i;
                    if (i % 5 == 0.0) {
                        if (i % 3 != 0.0) {
                            sum += i;
                        }
                    }
                }

            }

            System.out.println("The total is " + sum);
        }

    }
}

回答by PeterMmm

Try this:

尝试这个:

public class PrimeSum {

 public static void main(String args[]) {

    private double Num= 0.0;
    private double sum = 0.0;

    for(int i=0;i<1001;i++) {
        Num = i;
        if(i % 3 == 0.0) {
            sum += i;
            if(i % 5 == 0.0) {
                if(i % 3 != 0.0) {
                   sum += i;
                }
            }
        }
    }

    System.out.println("The total is "+ sum);
 }
}