因此,我很难理解何时使用静态以及何时将其从函数标头中排除

问题描述 投票:-2回答:1

在我的入门Java课程中,我们需要创建一个程序,该程序根据老师在班级网站上发布的内容来获取和设置一系列汽车的信息。要求之一是使函数能够打印所有三辆车的信息

public static void printAllCarsInfo() // function for printing info needed for all three cars
   {
      for ( int index = 0 ; index < NUM_CARS ; index++ )
         cars[index].printInfo();

printInfo()函数在另一个类中,看起来像:

   public void printInfo()
   {
      if ( fuelGallons > 0 )
         System.out.println("The " + getModel() + " has driven " + getMilesDriven() + " miles and has " + getFuelGallons() + " gallons left.");
      else
         System.out.println("The " + getModel() + " has driven " + getMilesDriven() + " miles and is out of gas.");

现在,当我调试它时,我收到一条错误消息:

Smith12.java:31: error: cannot find symbol
         cars[index].printInfo();
         ^
  symbol:   variable cars
  location: class Smith12
Smith12.java:36: error: cannot find symbol
         cars[index].drive(miles);
         ^
  symbol:   variable cars
  location: class Smith12
2 errors

((31和36是出现错误的行号)该错误来自for循环,但是即使在我将其切换为循环并将它们全部列出来之前,我也遇到了相同的错误。我已经尝试了很多事情,以至于我在这一点上还是被击败。有人请帮帮我。

这里是主要代码,因为它不太长:

import java.util.Scanner;

public class Smith12
{
   public static final int NUM_CARS = 3;
   
   public static void main(String[] args)
   {
      Car[] cars = new Car[NUM_CARS];
      cars[0] = new Car( "Toyota Camry", 3400 );
      cars[1] = new Car( "Ford F-150", 5000 );
      cars[2] = new Car( "Honda Civic", 3000 );
            
      for ( int index = 0 ; index < NUM_CARS ; index++ )
         cars[index].drive(10);
      
      printAllCarsInfo();
      
      cars[0].setFuelGallons(5);
      
      Scanner scanner = new Scanner(System.in);
      System.out.println("How many miles is your road trip? ");
      double roadTrip = scanner.nextDouble();
      
      driveAllCars( 10 );
      printAllCarsInfo();
   }
   public static void printAllCarsInfo() // function for printing info needed for all three cars
   {
      for ( int index = 0 ; index < NUM_CARS ; index++ )
         Car[index].printInfo();
   }
   public static void driveAllCars( double miles )
   {
      for ( int index = 0 ; index < NUM_CARS ; index++ )
         cars[index].drive(miles);
   }
}

这里是Car类:

public class Car
{
   private static String model;
   private static double mpg;
   private static double milesDriven;
   private static double fuelGallons;
   
   public Car( String carModel, double weight )
   {
      model = carModel;
      if ( weight > 4000 )
         mpg = 20.0;
      else
         mpg = 30.0;
      milesDriven = 7;
      fuelGallons = 15;
   }
   
   public String getModel()
   {
      return model;
   }
   
   public double getMPG()
   {
      return mpg;
   }
   
   public double getMilesDriven()
   {
      return milesDriven;
   }
   
   public double getFuelGallons()
   {
      return fuelGallons;
   }
   public void setFuelGallons( double gallons )
   {
      fuelGallons += gallons;
   }
   
   public void setMilesDriven( double distance )
   {
      milesDriven += distance;
   }
   
   public double getMilesLeft()
   {
      return mpg * fuelGallons;
   }
   
   public void drive( double miles )
   {
      if ( miles <= getMilesLeft() )
      {
         milesDriven += miles;
         fuelGallons -= ( miles / mpg );
      }
      else
      {
         milesDriven += getMilesLeft();
         fuelGallons = 0;
      }
   }
   public void printInfo()
   {
      if ( fuelGallons > 0 )
         System.out.println("The " + this.getModel() + " has driven " + this.getMilesDriven() + " miles and has " + this.getFuelGallons() + " gallons left.");
      else
         System.out.println("The " + this.getModel() + " has driven " + this.getMilesDriven() + " miles and is out of gas.");
   }
}

我已将某些变量更改为static,尽管我不确定它是否正确。我已经尝试过这些注释,但是我在这里寻求帮助,因为我总体上不太擅长编程。谢谢大家的帮助和评论。

java function static project main
1个回答
-1
投票

在静态函数中,您只能使用传递给此函数的其他静态变量和参数。没有this上下文。

© www.soinside.com 2019 - 2024. All rights reserved.