這裏是代碼想要一次又一次地在構造函數中進行輸入。我想要構造函數需要一次又一次地輸入
//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控制的重複來獲取來自用戶的數據 。
調用此構造函數在一個循環 –
刪除而從構造函數並在該循環中調用此構造函數 –
你的意思是構造函數將在循環內? –