IF ELSE声明方式

时间:2015-03-19 00:38:41

标签: java if-statement

我制作了一个根据用户输入计算总价的程序。它工作正常,但我想知道如何削减代码,但有相同的输出。

特别是在IF ELSE声明中,我想知道如何不在这些块中重复自己。有没有其他方法可以在IF ELSE块后写入输出,或者它们必须单独在这些块内?谢谢。 这是代码

import java.util.Scanner;
import java.text.*;

public class GasCalc
{
   public static void main(String[] args)`enter code here`
   {
      double gasPrice,carGallons,fullTank,totalPrice;


      Scanner input=new Scanner(System.in);
      System.out.print("Do you want to calculate full tank (y/n) ");
      String askMe=input.next();


      if
      (askMe.equalsIgnoreCase("y"))
      {
         DecimalFormat num=new DecimalFormat("$,###.00");
         System.out.print("What is the price of 1 gallon of gas ? ");
         gasPrice=input.nextDouble();
         System.out.print("How many gallons does your vehicle accept ? ");
         fullTank=input.nextDouble();
         totalPrice=gasPrice*fullTank;
         System.out.println("You will pay "+num.format(totalPrice)+" for the full tank of gas");
      }
      else
         if(askMe.equalsIgnoreCase("n"))
         {
            DecimalFormat num=new DecimalFormat("$,###.00");
            System.out.print("How many gallons do you need ? ");
            carGallons=input.nextDouble();
            System.out.print("What is the price of 1 gallon of gas ? ");
            gasPrice=input.nextDouble();
            totalPrice=gasPrice*carGallons;
            System.out.println("You will pay "+num.format(totalPrice)+" for "+carGallons+" gallons of gas");
         }
   }

}

3 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题 - 您不想在' if'中重复相同的代码。和其他' if陈述的一部分。

你这样做的方式与其他任何地方一样:提取公共代码作为从两个地方调用的函数/方法。

答案 1 :(得分:0)

您可以将最后两个语句移到外面,这样您的计算和打印就可以在if-else块之外完成。 您可以将fullTank和carGallons重命名为加仑,因此两个变量具有相同的名称,这样最后两个语句可以在if-else之外使用。

totalPrice=gasPrice*gallons;
System.out.println("You will pay "+num.format(totalPrice)+" for the full tank of gas");

答案 2 :(得分:0)

使用方法并分解ifelse中使用的局部变量num:

public class GasCalc {

    private static double readDouble(Scanner in, String msg) {
        System.out.print(msg);
        return in.nextDouble();
    }

    private static String readString(Scanner in, String msg) {
        System.out.print(msg);
        return in.next();
    }

    public static void main(String[] args) {
        double gasPrice, carGallons, fullTank, totalPrice;

        Scanner input = new Scanner(System.in);
        String askMe = readString(input,"Do you want to calculate full tank (y/n) ");
        DecimalFormat num = new DecimalFormat("$,###.00");

        if (askMe.equalsIgnoreCase("y")) {
            gasPrice = readDouble(input,"What is the price of 1 gallon of gas ? ");
            fullTank = readDouble(input,"How many gallons does your vehicle accept ? ");
            totalPrice = gasPrice * fullTank;
            System.out.println("You will pay " + num.format(totalPrice) + " for the full tank of gas");
        } else if (askMe.equalsIgnoreCase("n")) {
            carGallons = readDouble(input,"How many gallons do you need ? ");
            gasPrice = readDouble(input,"What is the price of 1 gallon of gas ? ");
            totalPrice = gasPrice * carGallons;
            System.out.println("You will pay " + num.format(totalPrice) + " for " + carGallons + " gallons of gas");
        }
    }
}

你实际上可以分解甚至更多但是为这种特定治疗创造另一种方法没有多大意义。如果您正在使用JDK 8并理解lambda表达式,那就去吧。