2016-09-09 4 views
-1

J'ai terminé beaucoup de code. Mais maintenant, à la fin, je ne peux pas comprendre comment obtenir le total MPG ajouté à partir de toutes les boucles. Im vraiment à une perte ici avec moi expérience limitée avec Java.Calculatrice Java mpg total

 //initialization phase 
    double stop = 1; 
    double miles = 0; 
    double gallons = 0; 
    double avgMPG = 0; 
    double totalMPG = 0; 
    double trip = 0; 

    // if the user enters zero it will stop the loop 
    while (stop != 0) 
    { 
     System.out.print("Enter a number for miles traveled, or enter 0 to exit: "); 
     miles = input.nextInt(); 

     if (miles == 0) 
     { 
      //took me a while to figure out where th break goes and {} placement 
      break; 
     } 
     else 
     { 
      System.out.print("Enter number of gallons used: "); 
      gallons = input.nextInt(); 
      avgMPG = miles/gallons; 
      trip++; 
      // i have tried multiple different ways to get totalMPG wont work 
      totalMPG = mpg++/trip++; 
     }        
    System.out.println("Your average MPG is " + avgMPG); 
    } 
    System.out.println("Your total trip MPG is " + totalMPG); 

} 

}

+0

Utilisez '' + =? De plus, l'incrément post ne donne pas de valeur incrémentée jusqu'à la référence suivante – Li357

+0

où devrais-je mettre + = at? –

+0

Au lieu de '=' – Li357

Répondre

0

Vous avez vraiment besoin de stocker vos totalMiles et totalGallons, essayez ceci:

//initialization phase 
    double stop = 1; 
    double totalMiles = 0; 
    double totalGallons = 0; 

    // if the user enters zero it will stop the loop 
    while (stop != 0) 
    { 
     System.out.print("Enter a number for miles traveled, or enter 0 to exit: "); 
     double newMiles = input.nextInt(); 

     if (newMiles == 0) 
     { 
      //took me a while to figure out where th break goes and {} placement 
      break; 
     } 
     else 
     { 
      System.out.print("Enter number of gallons used: "); 
      double newGallons = input.nextInt(); 

      // i have tried multiple different ways to get totalMPG wont work 
      totalMiles += newMiles; 
      totalGallons += newGallons; 

      System.out.println("Your average MPG is " + (newMiles/newGallons)); 
     } 
    } 

    System.out.println("Your total trip MPG is " + (totalMiles/totalGallons)); 
}