2015-12-28 89 views
-4

這裏是代碼想要一次又一次地在構造函數中進行輸入。我想要構造函數需要一次又一次地輸入

//gas class 

package gas.mileage; 

public class Gas { 
    int miles; // miles for one tankful 
     int gallons; // gallons for one tankful 
     int totalMiles = 0; // total mailes for trip 
     int totalGallons = 0; // total gallons for trip 

     double milesPerGallon; // miles per gallon for tankful 
     double totalMilesPerGallon; // miles per gallon for trip 

    public Gas(int miles,int gallons) { 

     // prompt user for miles and obtain the input from user 
     //System.out.print("Entered miles (-1 to quit): "); 
    // miles = input.nextInt(); 

     // exit if the input is -1 otherwise, proceed with the program 
     while (miles != -1) 
     { 
     // prompt user for gallons and obtain the input from user 
     //System.out.print("Entered gallons: "); 
     //gallons = input.nextInt(); 

     // add gallons and miles for this tank to total 
     totalMiles += miles; 
     totalGallons += gallons; 
      if(gallons!=0) 
      { 
       milesPerGallon=miles/gallons; 
       //System.out.println("miles pr gallon :"+milesPerGallon); 
      } 
      if(gallons!=0) 
      { 
       totalMilesPerGallon=totalMiles/totalGallons; 
      // System.out.println("total miles coverd using gallons :"+totalMilesPerGallon); 
      } 
     //use if statement to check if gallons is 0. 
     // caluclate miles per gallon for the current tank 
     // Print out the milesPerGallon 
    // Missing Part A 

// end of Missing Part A 

     //use if statement to check if totalGallons is 0. 
     // calculate miles per gallon for the total trip. 
     // Print out the totalMilesPerGallon 

// Missing Part B 

// End of Missing Part B 

     // prompt user for new value for miles 
     // System.out.print("Entered miles (-1 to quit): "); 
     break; 
     //miles = input.nextInt(); 
     } // end while loop  

} 
    public void setmpg(double milesPerGallon) 
    { 
     this.milesPerGallon=milesPerGallon; 
    } 
    public double getmpg(){ 
     return this.milesPerGallon; 
    } 
} 

// Main start 
package gas.mileage; 

import java.util.Scanner; 

public class GasMileage { 

    public static void main(String[] args) { 

    // perform miles per gallon calculations 

     Scanner input = new Scanner(System.in); 

    int a=input.nextInt(); 
    int b=input.nextInt(); 
    Gas g=new Gas(a, b); 
     Gas k=new Gas(60, 3); 
     Gas l=new Gas(20, 5); 


    System.out.println("vlaues are :"+g.getmpg()); 
    System.out.println("vlaues are :"+k.getmpg()); 
    System.out.println("vlaues are :"+l.getmpg()); 
    } // end main method 

} 

我想在這裏氣體類的構造函數的輸入不是一次又一次服用,一個enter code here LSO沒有找到和平均被totalmilesprgallon = totalmiles/totalgallons (汽油里程)驅動程序所關心的他們的汽車獲得里程。一位司機通過記錄每輛坦克所使用的里程驅動和加侖數,持續追蹤多次旅行。開發一個Java應用程序,該應用程序將輸入每次旅程所使用的驅動里程和加侖(兩者均爲整數)。 程序應該計算並顯示每次行程所獲得的每加侖里程數,並打印所有到此爲止的行程中獲得的每加侖總行駛里程數的組合公里數。所有的平均計算結果應該是 產生的浮點結果。使用classScannerand和sentinel控制的重複來獲取來自用戶的數據 。

+2

調用此構造函數在一個循環 –

+0

刪除而從構造函數並在該循環中調用此構造函數 –

+0

你的意思是構造函數將在循環內? –

回答

0

答案1:(緊湊的邏輯結構:沒有計算器,輸入里程和加侖以及每行程打印概覽)。

public static void main(String[] args) { 
    Scanner sc = new Scanner(System.in); 
    while(moreTrip(sc, "Would you like another calculation? (Y or N) ")) { 
     int miles, gallons; 
     while ((miles = getInt(sc, "Miles: ")) < 0) { 
      System.out.println("Please enter a non-negative number"); 
     } 
     while ((gallons = getInt(sc, "Gallons: ")) <= 0) { 
      System.out.println("Please enter a positive number"); 
     } 
     System.out.println((new Trip(miles, gallons)).getMPG()); 
    } 
} 

private static boolean moreTrip(Scanner sc, String message) { 
    System.out.print(message); 
    String response; 
    if ((response = sc.nextLine()).isEmpty()) { 
     response = sc.nextLine(); 
    } 
    return ("y".compareToIgnoreCase(response) == 0) 
      ? true 
      : (("n".compareToIgnoreCase(response) == 0) 
       ? false 
       : moreTrip(sc, message)); 
} 

private static int getInt(Scanner sc, String message) { 
    System.out.print(message); 
    return (sc.hasNextInt()) ? sc.nextInt() : getInt(sc, "Please enter a number"); 
} 

public class Trip { 
    private final int miles; 
    private final int gallons; 

    public Trip(int miles, int gallons) { 
     this.miles = miles; 
     this.gallons = gallons; 
    } 

    public double getMPG() { 
     return (double)miles/gallons; 
    } 
} 

附:情況並不清楚。所以,如果你想多行程統計信息添加到您的類,並根據這些統計資料計算,你最好做一個Trip類,而不是GasTripCalculator類與適當的職能範圍內做出產權private final List<Trip> trips = new ArrayList<>();(見答案2) 。

0

答2:完整結構(收集多個跳閘細節,使用TripCalculator打印行程集合的所有細節)

Trip類表示用於單個行程英里,加侖:

public class Trip { 
    private final int miles; 
    private final int gallons; 

    public Trip(int miles, int gallons) { 
     this.miles = miles; 
     this.gallons = gallons; 
    } 

    public double getMPG() { 
     return (double)miles/gallons; 
    } 

    public int getMiles() { 
     return miles; 
    } 

    public int getGallons() { 
     return gallons; 
    } 

    @Override 
    public String toString() { 
     StringBuilder sb = new StringBuilder(); 
     return sb.append("\nMiles: ").append(miles) 
       .append("\nGallons: ").append(gallons) 
       .append("\nMiles per Gallon: ").append(getMPG()) 
       .toString(); 
    } 
} 

GasCalculator這個類包含(多個)旅程和函數的集合,以獲得所有行程的總里程數,所有行程的總加侖數,以及ave風靡全球的每加侖總里程:

import java.util.ArrayList; 
import java.util.List; 

public class TripCalculator { 
    private final List<Trip> trips = new ArrayList<>(); 

    public void add(Trip trip) { 
     trips.add(trip); 
    } 

    public int getTotalMiles() { 
     return trips.stream().mapToInt(trip -> trip.getMiles()).sum(); 
    } 

    public int getTotalGallons() { 
     return trips.stream().mapToInt(trip -> trip.getGallons()).sum(); 
    } 

    public double getAverageMPG() { 
     return (double)getTotalMiles()/getTotalGallons(); 
    } 

    public void printTrips() { 
     trips.stream().forEach(System.out::println); 
    } 

    public void printAll() { 
     System.out.println("\nSTATISTICS"); 
     printTrips(); 
     System.out.println(this); 
    } 

    @Override 
    public String toString() { 
     StringBuilder sb = new StringBuilder(); 
     return sb.append("\nTotal Miles: ").append(getTotalMiles()) 
       .append("\nTotal Gallons: ").append(getTotalGallons()) 
       .append("\nAverage Miles per Gallon: ").append(getAverageMPG()) 
       .toString(); 
    } 
} 

只是一個測試驅動器:

import java.util.Scanner; 

public class TestDrive { 

    public static void main(String args[]) { 
     Scanner sc = new Scanner(System.in); 
     TripCalculator calc = new TripCalculator(); 

     while(moreTrip(sc, "Would you like another calculation? (Y or N) ")) { 
      int miles, gallons; 
      while ((miles = getInt(sc, "Miles: ")) < 0) { 
       System.out.println("Please enter a non-negative number"); 
      } 
      while ((gallons = getInt(sc, "Gallons: ")) <= 0) { 
       System.out.println("Please enter a positive number"); 
      } 
      calc.add(new Trip(miles, gallons)); 
     } 

     calc.printAll(); 
    } 

    private static boolean moreTrip(Scanner sc, String message) { 
     System.out.print(message); 
     String response; 
     if ((response = sc.nextLine()).isEmpty()) { 
      response = sc.nextLine(); 
     } 
     return ("y".compareToIgnoreCase(response) == 0) 
       ? true 
       : (("n".compareToIgnoreCase(response) == 0) 
        ? false 
        : moreTrip(sc, message)); 
    } 

    private static int getInt(Scanner sc, String message) { 
     System.out.print(message); 
     return (sc.hasNextInt()) ? sc.nextInt() : getInt(sc, "Please enter a number"); 
    } 
} 

那麼你也可以添加一些動力工具,將TripCalculator,例如像:

public Trip toTrip() { 
    return new Trip(getTotalMiles(), getTotalGallons()); 
} 

public void reset() { 
    trips.clear(); 
} 

public Trip resetAndGet() { 
    Trip trip = toTrip(); 
    reset(); 
    return trip; 
} 

public void resetAndContinue() { 
    trips.add(resetAndGet()); 
} 
相關問題