虽然Loop没有测试条件

时间:2017-08-09 22:01:15

标签: java loops repeat

我想知道为什么while循环没有正确评估表达式。似乎表达式总是计算为false。我想知道我是否没有正确使用扫描仪对象?

import java.util.Scanner;
public class GasMileage
{
public static void main(String [] args)
{
  int milesDriven =0;
  int gallonUsed =0;
  float mpg = 0;
  int tripNum = 1;
  int totalMilesDriven =0;
  String flag = "yes";

  Scanner input = new Scanner(System.in);

  do
  {  
     //Program Header
     System.out.println("Trip " + tripNum);

     //Promp user to Enter Data
     System.out.println("What are the miles driven for trip " + tripNum + "?");
     milesDriven = input.nextInt();

     System.out.println("What are gallons used for the trip?");
     gallonUsed = input.nextInt();

     //Calcualte the mpg of the trip
     mpg = (float)milesDriven / gallonUsed;
     System.out.println("The Miles per Gallon obtained in this trip is " + mpg);

     //Keep track of total miles drivesn 
     totalMilesDriven += milesDriven;
     System.out.println("Total Miles driven is " + totalMilesDriven + "\n");

     //Determine if iteration should continue
     System.out.println("Will there be another trip? Enter yes or no");
     flag = input.next();
     tripNum++;

     System.out.println(flag);

  }
  while( flag == "yes" );

 System.out.println("Exiting the program"); 

} }

0 个答案:

没有答案