0

我从来没有遇到过这个问题,因为没有给出错误,我什至不确定在哪里看。我用这个程序玩得很开心(这是我第一次做方法)。在此问题之前,它一直在抛出异常。虽然我研究了那篇文章并设法修复了错误,但我觉得它不正确。请让我知道整个代码是否关闭(最好是一些建设性的批评)或者我是否接近:

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

4 回答 4

3
 public void InsuranceMethod2() throws IOException{ //constructor

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

你要

 public InsuranceMethod2() throws IOException{ //constructor

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

您还应该使从构造函数调用的所有方法私有或至少是最终的。

于 2013-09-06T01:23:59.093 回答
1

您的程序唯一要做的就是创建 InsuranceMethod 类的新实例。然后这个类的构造函数运行,因为你还没有定义一个(你定义的方法有一个返回类型,使它不是一个构造函数),默认的构造函数运行(并且什么都不做)。因此,您看不到任何输出。如果您希望以相同的名称调用该方法,请编写:

new InsuranceMethod2().InsuranceMethod2();

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

于 2013-09-06T01:23:50.890 回答
1

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

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

public InsuranceMethod2() throws IOException{ ... }

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

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

于 2013-09-06T01:25:12.723 回答
0

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

public InsuranceMethod2() throws IOException{ ... }

为了解决其他误解(在哪里声明变量、样式等),我用注释重写了您的代码(查找 LEBOLO)。有关样式的详细信息,最好只搜索“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
于 2013-09-06T02:11:33.120 回答