所以我想打印这段代码..它说:
非静态方法降雨无法从静态环境中访问。
问题是打印所有月份的名称及其在平行列中的降雨量及其总平均值和降雨量最高和最低的月份。
请帮帮我。
public class rainfall
{
String[] rain = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
double[] values = { 0.40,0.94,3.21,3.74,1.73,1.03,1.27,2.58,6.98,6.90,2.80,2.53};
public double Total( double total )
{
total = 0;
for( int i = 0 ; i < values.length ; i ++ )
{
total += values[i];
}
return total;
}
public double Average( double average)
{
double total = 0;
Total (total);
for ( int j = 0; j < values.length; j ++ )
{
average = total/values.length ;
}
return average;
}
public String mostRain (double high,String highest )
{
high = values[0];
for(int i = 1 ; i < rain.length ; i ++ )
{
if ( values[i]>high )
{
highest = rain[i];
}
return highest;
}
}
public String leastRain ( String least, double low )
{
least = "";
low= values[0];
for(int i = 1; i < rain.length ; i++ )
{
if ( values[i]<low )
{
least = rain[i];
}
return least;
}
}
public static void main(String[]Args)
{
String highest = "" ;
String least = "";
double total = 0 ;
double average = 0 ;
double high = 0;
double low = 0;
System.out.println(" AUSTIN Tx RAINFALL 2009 " );
for( int i = 0 ; i < rain.length ; i ++ )
{
System.out.println(rain[i]+"\t"+values[i]);
}
System.out.println(Total(total));
System.out.println(Average(average));
System.out.println(mostRain(highest,high));
System.out.println(leastRain(least,low));
}
}
答案 0 :(得分:0)
无法在main
等静态方法中访问实例变量。您必须使所有字段和方法都是静态的。
static String[] rain = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
static double[] values;
public static String mostRain(...
public static String leastRain(...
答案 1 :(得分:0)
您的问题是您的主要方法是static
,但其他方法和变量不是。
您需要创建rainfall
的实例并调用该实例中的方法,或者使所有方法和变量也是静态的。
答案 2 :(得分:0)
更改此
String[] rain = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
double[] values = { 0.40,0.94,3.21,3.74,1.73,1.03,1.27,2.58,6.98,6.90,2.80,2.53};
到这个
static String[] rain = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
static double[] values = { 0.40,0.94,3.21,3.74,1.73,1.03,1.27,2.58,6.98,6.90,2.80,2.53};
或
将(非静态)变量放入main函数本身,然后根据需要将它们传递给函数。如
public static final void main(String[] ignored) {
String[] rain = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
double[] values = { 0.40,0.94,3.21,3.74,1.73,1.03,1.27,2.58,6.98,6.90,2.80,2.53};
Total(values);
如其他人所说,main调用的所有函数也需要是静态的,或者通过新实例调用。
(请将函数名称更改为小写。)
答案 3 :(得分:0)
您需要将
每当您不使用您正在使用的类中定义的属性时,您应该始终考虑将方法设置为静态(例如,计算平均值的方法,通常仅取决于您作为参数获得的值)。 / p>
答案 4 :(得分:0)
您正在静态上下文中调用方法:
System.out.println(Average(...));
这意味着您在对象上下文中使用该方法而不使用它,即object.Average(...)
。
你必须
声明方法是静态的:
public static double Average(...){
或者在对象上使用该方法:
YourObject obj = new YourObject(); obj.Average(...);
PS:方法应该以小写字符开头。
答案 5 :(得分:0)
由于这些方法是非静态的,因此只能通过该类的对象访问它们。运行静态方法时,它们不会通过实例化对象运行,因此它们无法访问非静态方法。
那么你要做的是在main中创建一个Rainfall对象并调用该对象上的方法。这通常是怎么做的。通常,main()方法本身并没有做太多工作,但会创建一个对象并调用该对象上的方法来完成实际的工作。
所以你会做类似的事情:
public class Rainfall {
//your methods from above
public static void main(String[]Args) {
String highest = "" ;
String least = "";
double total = 0 ;
double average = 0 ;
double high = 0;
double low = 0;
Rainfall rain = new Rainfall();
System.out.println(" AUSTIN Tx RAINFALL 2009 " );
for( int i = 0 ; i < rain.length ; i ++ ) {
System.out.println(rain[i]+"\t"+values[i]);
}
System.out.println(rain.Total(total));
System.out.println(rain.Average(average));
System.out.println(rain.mostRain(highest,high));
System.out.println(rain.leastRain(least,low));
}
}