簡體   English   中英

java中的距離旅行程序

[英]Distance traveled program in java

我得到了部分完成的代碼來完成,但我不知道接下來要做什么。 該程序要求用戶輸入以確定行駛距離。
我填寫了大部分代碼,但我似乎無法弄清楚要添加什么才能讓 getDistance() 工作。 有人可以給我一個建議嗎?

指令是:

車輛行駛的距離可以計算如下: 距離 = 速度 * 時間 例如,如果火車以每小時 40 英里的速度行駛三個小時,則行駛距離為 120 英里。 編寫一個程序,詢問車輛的速度(以 mph 為單位)及其行駛的小時數。 它應該使用循環來顯示車輛在用戶指定的時間段內每小時行駛的距離。 例如,如果一輛車以 40 mph 的速度行駛了三個小時,它應該顯示類似於以下內容的報告:Hour Distance Traveled

//Distance.java

public class Distance
{
   private double speed;   // The vehicle speed
   private int hours;      // The hours traveled

   /**
    * The constructor initializes the object
    * with a vehicle speed and number hours
    * it has traveled.
    */

   public Distance(double s, int h)
   {

   }

   /**
    * The setSpeed method sets the vehicle's
    * speed.
    */

   public void setSpeed(double s)
   {
       speed = s;
   }

   /**
    * The setHours method sets the number of
    * hours traveled.
    */

   public void setHours(int h)
   {
       hours = h;
   }

   /**
    * The getSpeed method returns the speed
    * of the vehicle.
    */

   public double getSpeed()
   {
       return speed;
   }

   /**
    * The getHours method returns the number
    * of hours traveled by the vehicle.
    */

   public int getHours()
   {
       return hours;
   }

   /**
    * The getDistance method returns the
    * distance traveled by the vehicle.
    */

   public double getDistance()
   {
       return speed * hours;
   }
}



//DistanceDemo.java

public class DistanceDemo
{
   public static void main(String[] args)
   {
      double speed;     // Vehicle's speed
      int maxHours;     // Number of hours
      int period;       // Counter for time periods

      // Create a Scanner object for keyboard input.
      Scanner keyboard = new Scanner(System.in);
  
      // Get the speed.
      System.out.print("Enter the vehicle's speed: ");
      speed = keyboard.nextDouble();
  
      // Validate the speed.
      while (speed < 1)
      {
          System.out.println("Enter a valid speed: ");
          speed = keyboard.nextDouble();
      }
  
      // Get the number of hours.
      System.out.print("Enter the number of hours the " +
                   "vehicle was in motion: ");
      maxHours = keyboard.nextInt();

      // Validate the hours.
      while (maxHours < 1)
      {
         System.out.println("Enter a vaid time");
         maxHours = keyboard.nextInt();
      }
  
      // Display the table header.
      System.out.println("Hour\tDistance Traveled");
      System.out.println("----------------------------------");
  
      // Display the table of distances.
      period = 1;
      while (period <= maxHours)
      {
         // Create a Distance object for this period.
         Distance d = new Distance(speed, period);
     
         // Display the distance for this period.
         System.out.println(period + "\t\t" + d.getDistance());
     
         // Increment period.
         period++;
      }
   }
}

您需要填寫距離的構造函數來設置速度和小時的值

public Distance(double s, int h) {
  speed = s;
  hours = h;
}

在主函數中,您正在使用構造函數設置速度和小時數。

Distance d = new Distance(speed, period);

但是在距離類中,您沒有在構造函數中初始化變量速度和小時。

   public Distance(double s, int h)
   {

   }

所以,把這個構造函數寫成:

public Distance(double s, int h)
   {
      speed = s;
      hours = h;
   }

在這里試試這個,

更改距離構造函數以在創建對象期間設置速度和小時數

public Distance(double s, int h)
{
   speed = s;
   hours = h;
}

將距離方法更改為

public double getDistance(int hours)
{
   return speed * hours;
}

並替換

// Display the table of distances.
  period = 1;
  while (period <= maxHours)
  {
     // Create a Distance object for this period.
     Distance d = new Distance(speed, period);
 
     // Display the distance for this period.
     System.out.println(period + "\t\t" + d.getDistance());
 
     // Increment period.
     period++;
  }

Distance d = new Distance(speed,maxHours);
      
      for (int i = 1;i<=maxHours;i++) {
        System.out.println("Hour "+i+" "+"Distance Traveled"+" "+d.getDistance(i));  


     

無需與額外的變量復雜化,只需更改參數為 a hour 的 getDistance 方法,它就會返回某個 Hour 的距離,並且 for 循環將小時從 1 更改為當前小時將為您提供准確的結果。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM