简体   繁体   English

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

[英]Program compiles and runs but doesn't output anything

I've never had this issue and since there are no errors given, I am unsure of where to even look. 我从来没有遇到过这个问题,并且由于没有给出错误,所以我不确定在哪里看。 I've had a heck of a time with this program (it is my first time doing methods). 我用这个程序已经很久了(这是我第一次做方法)。 Prior to this issue, it kept throwing exceptions. 在此问题之前,它一直抛出异常。 While I researched that piece, and managed to fix the error, I get the feeling it isn't right. 当我研究该部分并设法纠正错误时,我感到这是不对的。 Please let me know if the entire code is off (preferably some constructive criticism) or if I'm close: 请让我知道整个代码是否已关闭(最好是一些建设性的批评)或是否接近:

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
 public void InsuranceMethod2() throws IOException{ //constructor

That is not a constructor. 那不是构造函数。 It's just a method (with a very confusing name). 这只是一个方法(名称非常混乱)。

You want 你要

 public InsuranceMethod2() throws IOException{ //constructor

Without that, you just get a default constructor that does nothing. 否则,您将只获得不执行任何操作的默认构造函数。

You should also make all methods called from a constructor private or at least final. 您还应将所有从构造函数调用的方法设为私有或至少为final。

The only thing your program does is create a new instance of the InsuranceMethod class. 程序唯一要做的就是创建InsuranceMethod类的新实例。 This class's constructor then runs, and since you haven't defined one (your defined method has a return type which makes it not a constructor), the default constructor runs (and does nothing). 然后,该类的构造函数将运行,并且由于您尚未定义一个构造函数(您定义的方法具有返回类型,因此它不是构造函数),因此默认构造函数将运行(不执行任何操作)。 Therefore, you are seeing no output. 因此,您看不到任何输出。 If you wish to call the method by the same name, write: 如果要使用相同的名称调用该方法,请输入:

new InsuranceMethod2().InsuranceMethod2();

Otherwise you should remove the void keyword from your method to turn it into a constructor. 否则,应从方法中删除void关键字,以将其转换为构造函数。

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

The code above is not a constructor, as it is a method declaration. 上面的代码不是构造函数,因为它是方法声明。 If you want it to be a constructor, remove the void , so it will look something like this: 如果希望它成为构造函数,请删除void ,这样它将类似于以下内容:

public InsuranceMethod2() throws IOException{ ... }

Also, you have already globally declared your variables, there is no need to keep declaring them again in each method because when you try referencing that variable, it will reference the locally declared variable as opposed to the globally declared variable, so you will receive unexpected results. 另外,您已经全局声明了变量,因此无需在每个方法中再次声明它们,因为当您尝试引用该变量时,它将引用本地声明的变量,而不是全局声明的变量,因此您将收到意外的消息。结果。

As a tip though, if you have field names that are common within your global variable declarations and your local variable declarations, you can reference the global variable by suffixing a this. 不过,作为提示,如果您在全局变量声明和局部变量声明中具有通用的字段名称,则可以通过在this.后缀this.来引用全局变量this. before it. 在它之前。

There seems to be a few misconceptions within your code. 您的代码中似乎存在一些误解。 To answer your direct question, a constructor doesn't have a return type (see this tutorial ). 为了回答您的直接问题,构造函数没有返回类型(请参阅本教程 )。 So simply change your constructor (which in your case is actually a method) to 因此,只需将您的构造函数(在您的情况下实际上是一个方法)更改为

public InsuranceMethod2() throws IOException{ ... }

To address the other misconceptions (where to declare variables, styling, etc.), I rewrote your code a bit with comments (look for LEBOLO). 为了解决其他误解(在何处声明变量,样式等),我用注释重新编写了您的代码(查找LEBOLO)。 For details on styling, it's best to just Google "java code style" or "java code conventions". 有关样式的详细信息,最好仅使用Google“ java代码样式”或“ java代码约定”。 I hope this helps and points you towards more learning resources! 希望对您有所帮助,并为您提供更多学习资源! Have fun! 玩得开心!

Rewritten Code 改写代码

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

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

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