0

我不断收到消息

线程“main”中的异常 java.lang.NoSuchMethodError: WeatherInput: method ()V not found

在我正在从事的这个项目的客户课程中,我不知道为什么,我想知道这里是否有人能看到我缺少的东西?

这是我的服务类:

class WeatherInput
{

 private static final String NEWLINE = "\n";
 private double temp;
 private double tempAverage;
 private double tempHigh;
 private double tempLow;
 private double tempHundred;
 public  String newCity = new String();
 public  String city = new String();


public WeatherInput( String city) {
 temp = 0;
 tempAverage = 0;
 tempHigh = 0;
 tempLow = 0;
 tempHundred = 0;
 newCity = city;
}


public void setTemp( double temprature)
{  
    temp = temp;
    } 

        public void tempCalc(String city)
        {
            while(!city.equals("*"))
            {   

            city = newCity;
        }
                while(temp != 0)
                {

                tempAverage = (temp + temp)/2;

                if(tempHigh > temp)

                tempHigh = temp;


                if(tempLow < temp)

                tempLow = temp;

            if(temp > 100)

                tempHundred = temp;

                temp++;
                }
            System.out.print(tempAverage);
            }

        public String printString(){
        return NEWLINE + "Statistics for: " + newCity + "Average: " + tempAverage + "High: " + tempHigh + "Low: " + "Over 100: "
        + tempHundred + NEWLINE;
        }
    }

这是我的客户:

import java.util.*;       //For Scanner and class                    
//----------------------------------------------------------------------------------
//Class Name:  KosmowsiProg2
//Description: This class has one method, the main method
//----------------------------------------------------------------------------------
public class KosmowskiProg2
{
    //-------------------------------------------------------------------------------
    //Method Name: main

//-------------------------------------------------------------------------------
public static void main(String[] args)
{
    //-----------------------Local Constants--------------------------------------


    //-----------------------Local Variables--------------------------------------
        double  newTemp;


    //-----------------------Objects----------------------------------------------
        Scanner scanner = new Scanner(System.in);
        String city = new String();
        String inputCity = new String();
        WeatherInput input = new WeatherInput();
        WeatherInput input2 = new WeatherInput();


    //---------------------Method Body--------------------------------------------

    System.out.print("Please enter the names of the cities, ending in a *");
    inputCity = scanner.next();
    System.out.print("Please enter the tempratures, ending in a 0");
    newTemp = scanner.nextDouble();


       input.setTemp(newTemp);
        input.tempCalc(inputCity);  




}//End main method


private static void printResults(WeatherInput in){

System.out.print(in.printString()); 
}
}//End class KosmowskiProg2

我正在创建一个服务类,然后我可以将其传递给客户端类,该类将读取以“*”结尾的城市列表,然后为每个城市读取以“0”结尾的温度列表。然后它必须计算平均温度,最高和最低,然后将它们与任何超过 100 的温度一起输出。我和我的教授谈过,她告诉我解决方案是一个嵌套的 while 循环,并且提示最终程序应该在一个单独的客户端类中。我还需要输出每个类别的总数。问题是,我在试图弄清楚如何使用我在客户端中创建的这个服务类来获取我需要的数据时遇到了很多麻烦。我的教授似乎无法以我能理解的方式解释它,所以我希望能得到一些帮助。

4

2 回答 2

0

如果你的服务类在不同的文件中,你需要确保它被声明为公共的。

public class WeatherInput
{ 
    ... etc 
}

如果不这样做,您可能无法访问其他文件中的构造函数。

编辑:哦,我刚刚注意到,您没有默认构造函数(没有参数的构造函数),这就是它引发 NoSuchMethodError 异常的原因。您唯一的构造函数需要一个城市名称:

public WeatherInput(String city) {
    ... etc
}

获得城市名称后,您需要声明您的课程:

// WeatherInput input = new WeatherInput();
// WeatherInput input2 = new WeatherInput();


//---------------------Method Body--------------------------------------------

System.out.print("Please enter the names of the cities, ending in a *");
inputCity = scanner.next();
System.out.print("Please enter the tempratures, ending in a 0");
newTemp = scanner.nextDouble();

WeatherInput input = new WeatherInput(inputCity);

input.setTemp(newTemp);
input.tempCalc(inputCity);  

或者创建一个不带任何参数的构造函数:

public WeatherInput() {
   temp = 0;
   tempAverage = 0;
   tempHigh = 0;
   tempLow = 0;
   tempHundred = 0;
   newCity = null;
}
于 2013-04-19T00:46:14.160 回答
0

WeatherInput需要 aString作为其构造函数的一部分...

public WeatherInput( String city)

但你正试图在没有任何...

WeatherInput input = new WeatherInput(); // This is bad, must have a String value...
于 2013-04-19T00:47:37.837 回答