程序编译并运行但不输出任何内容

时间:2013-09-06 01:20:46

标签: java methods compiler-construction

我从来没有遇到过这个问题,因为没有给出任何错误,我不确定在哪里看。我对这个程序有点兴奋(这是我第一次做方法)。在此问题之前,它一直在抛出异常。当我研究那件作品并设法修复错误时,我感觉这是不对的。请告诉我整个代码是否已关闭(最好是一些建设性的批评)或者我是否接近:

import java.io.*;

public class InsuranceMethod2//class name here, same as file name

{   

// use BufferedReader class to input from the keyboard
// declare a variable of type BufferedReader
private BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
//declare variable for input
private String inputString;

    String carChoice;
    boolean insurable;
    int year;

public void InsuranceMethod2()throws IOException{//constructor, place class name here

String carChoice;
boolean insurable;
int year; 

initialize();
insureProcess();
cleanUp();
}//end constructor

public void initialize(){
    System.out.println();
    System.out.println("Insurance Process Intializing");
}//end initialize

public void insureProcess() throws IOException {

    String carChoice;
    boolean insurable;
    int year;

    System.out.println("Enter your vehicle model: ");
    inputString = input.readLine();
    carChoice = inputString;
            if(carChoice.equalsIgnoreCase("ford") || carChoice.equalsIgnoreCase("chevy") || carChoice.equalsIgnoreCase("toyota"))
    {
        System.out.println("Enter the vehicle year: ");
        inputString = input.readLine();
        year = Integer.parseInt(inputString);
        if(year >= 1990)
        {
            System.out.println("Your vehicle is insurable");
        }
    }
}

public boolean checkModel(){
    if(carChoice.equalsIgnoreCase("Ford")|| carChoice.equalsIgnoreCase("Chevy")|| carChoice.equalsIgnoreCase("Toyota"))
    {
        return true;
    }
    else
    {
        return false;
    }
}//end checkModel

public boolean checkYear(int year){
    return false;
}//end checkYear

public void printResults(boolean insurable){

}//end printResults

public void cleanUp(){
    System.out.println("Program ending");
}//end cleanUp

public static void main(String [] args) throws IOException // main method

{
    new InsuranceMethod2(); //class constructor name
} // end the main method
} // end the program

4 个答案:

答案 0 :(得分:3)

 public void InsuranceMethod2() throws IOException{ //constructor

那不是构造函数。这只是一种方法(名称非常混乱)。

你想要

 public InsuranceMethod2() throws IOException{ //constructor

如果没有它,你只需得到一个什么都不做的默认构造函数。

您还应该将所有从构造函数调用的方法设置为private或至少是final。

答案 1 :(得分:1)

您的程序唯一能做的就是创建InsuranceMethod类的新实例。然后运行此类的构造函数,并且由于您尚未定义一个(您定义的方法具有使其不是构造函数的返回类型),因此默认构造函数运行(并且不执行任何操作)。因此,您没有看到任何输出。如果您希望以相同的名称调用该方法,请写:

new InsuranceMethod2().InsuranceMethod2();

否则,您应该从方法中删除void关键字,将其转换为构造函数。

答案 2 :(得分:1)

public void InsuranceMethod2()throws IOException{//constructor, place class name here

上面的代码不是构造函数,因为它是方法声明。如果您希望它是构造函数,请删除void,因此它看起来像这样:

public InsuranceMethod2() throws IOException{ ... }

此外,您已经全局声明了变量,不需要在每个方法中再次声明它们,因为当您尝试引用该变量时,它将引用本地声明的变量而不是全局声明的变量,因此您会收到意想不到的结果

作为提示,如果您的全局变量声明和局部变量声明中有通用的字段名称,则可以通过在其前面添加this.后缀来引用全局变量。

答案 3 :(得分:0)

您的代码中似乎存在一些误解。要回答直接问题,构造函数没有返回类型(请参阅this tutorial)。因此,只需将构造函数(在您的情况下实际上是一个方法)更改为

public InsuranceMethod2() throws IOException{ ... }

为了解决其他误解(在哪里声明变量,样式等),我用注释重写了你的代码(寻找LEBOLO)。有关样式的详细信息,最好只使用Google“java代码样式”或“java代码约定”。我希望这有助于您指出更多的学习资源!玩得开心!

重写代码

import java.io.*;

public class InsuranceMethod2 { //class name here, same as file name   

    // use BufferedReader class to input from the keyboard
    // declare a variable of type BufferedReader
    private BufferedReader input = new BufferedReader( new InputStreamReader(System.in) );

    // declare variable for input
    //private String inputString; // LEBOLO: Remove, don't need (see larger comment below for details)

    /* LEBOLO
     *
     * Do you want all of these as class level variables? I assume you don't since you don't use them all.
     * In general, you only put variables here (called instance variables) if you want multiple methods to share them.
     * You don't have to declare variables here just because one method uses them.
     * See http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html for details.
     *
     * Also, by not stating a modifier (e.g. private or public), Java defaults to the package access level.
     * See http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html for details
     */
    String carChoice; // LEBOLO: I think you want this private
    //boolean insurable; // LEBOLO: Remove, don't need
    //int year; // LEBOLO: Remove, don't need

    // LEBOLO: Removed the void return type.
    // See http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html for details.
    public InsuranceMethod2() throws IOException { // constructor, place class name here

        //String carChoice; // LEBOLO: Remove, don't need
        //boolean insurable; // LEBOLO: Remove, don't need
        //int year; // LEBOLO: Remove, don't need

        initialize();
        insureProcess();
        cleanUp();
    } // end constructor

    public void initialize() {
        System.out.println();
        System.out.println("Insurance Process Intializing");
    } // end initialize

    public void insureProcess() throws IOException {

        /* LEBOLO
         *
         * Since you use the 'year' variables here, it makes sense to declare it here (local variables).
         * You don't use 'insurable' and 'carChoice' is an instance (class level) variable, so no need to declare here.
         */

        //String carChoice; // LEBOLO: Remove, don't need (since using the instance variable)
        //boolean insurable; // LEBOLO: Remove, don't need
        int year;

        System.out.println("Enter your vehicle model: ");

        // LEBOLO: Declare inputString here, since you only use it here
        String inputString = input.readLine();
        carChoice = inputString;

        if (carChoice.equalsIgnoreCase("ford") || carChoice.equalsIgnoreCase("chevy") || carChoice.equalsIgnoreCase("toyota")) {
            System.out.println("Enter the vehicle year: ");
            inputString = input.readLine();

            year = Integer.parseInt(inputString);
            if (year >= 1990) {
                System.out.println("Your vehicle is insurable");
            }
        }
    }

    public boolean checkModel() {

        if(carChoice.equalsIgnoreCase("Ford")|| carChoice.equalsIgnoreCase("Chevy")|| carChoice.equalsIgnoreCase("Toyota")) {
            return true;
        }
        else
        {
            return false;
        }

        /* LEBOLO: A cleverer way would be
         *
         * return (carChoice.equalsIgnoreCase("Ford")|| carChoice.equalsIgnoreCase("Chevy")|| carChoice.equalsIgnoreCase("Toyota"));
         */
    }//end checkModel

    /* LEBOLO
     *
     * If you want to use the shared year, then declare the variable at the class level (instance variable)
     * and remove it from the method signature (instead using it within the method body).
     *
     * public boolean checkYear() {
     *     return (year >= 1990);
     * } // end checkYear
     *
     * If you want the user to say checkYear(1993), then leave this as is.
     */
    public boolean checkYear(int year) {
        return false;
    } // end checkYear

    /* LEBOLO
     * 
     * See my comment for checkYear(...)
     */
    public void printResults(boolean insurable) {
    } // end printResults

    public void cleanUp() {
        System.out.println("Program ending");
    } // end cleanUp

    public static void main(String [] args) throws IOException { // main method
        new InsuranceMethod2(); // class constructor name
    } // end the main method
} // end the program