java - 如何更新和打印我的 "estimatedCost"Double? Java新手

标签 java eclipse

//Import scanner
import java.util.Scanner;

public class PrintCostCalculator{

public static void main(String[] args)
{
    //Define variables
    Scanner keyboard = new Scanner(System.in);
    final double text = 5000; //Text dots per page
    final double image = 10000; //Image dots per page
    final double cText = 15000; //Compressed text dots per page
    final double statement = 7000; //Statement dots per page
    final double color = (5e-5); //Color ink cost per dots
    final double black = (1e-5); //Black ink cost per dots
    Double estimatedCost = 0.0;


    //Pages to print
    System.out.printf("--- Price Estimator Program ---%nEnter Number of Pages (digits only):  ");
    int pagestoPrint = keyboard.nextInt();

    //Print Type
    System.out.printf("%n---- Select a Print Type ----%nEnter T or t for Text%nEnter I or i for Image%nEnter C or c for Compressed Text%nEnter S or s for statement%n---------------------------%nEnter Print Type:  ");
    String pType = keyboard.next(); //pType is print type variable holder
    char PrintType = pType.charAt(0);

    //Print Color
    System.out.printf("%n--- Select a Print Color ---%nEnter 0 for Grayscale%nEnter 1 for Color%n-----------------------------%nEnter Print Color:  ");
    int printColor = keyboard.nextInt();
    System.out.printf("-----------------------------%nIs there a sale (y/n): ");
    String sType = keyboard.next(); //sType is the sale type variable holder
    String lower =sType.toLowerCase();
    char saleType = sType.charAt(0);
    System.out.print(lower);

    //Calculation for color printing
        if (printColor == 1 && lower == "t") {
                 estimatedCost = pagestoPrint * text / color;
            } else { 
                if (printColor == 1 && lower == "i") {
                     estimatedCost = pagestoPrint * image / color;
                } else {
                    if  (printColor == 1 && lower == "c") {
                         estimatedCost = pagestoPrint * cText / color;
                    } else {
                        if (printColor == 1 && lower == "s") {
                             estimatedCost = pagestoPrint * statement / color;
                    //  } else {
                        //  System.out.println("oops");
                        }
                    }
                }
            }

    //Calculation for black printing
                    if (printColor == 0 && lower == "t") {
                                 estimatedCost = pagestoPrint * text / black;
                        } else { 
                            if (printColor == 0 && lower == "i") {
                                 estimatedCost = pagestoPrint * image / black;
                        } else {
                            if  (printColor == 0 && lower == "c") {
                                 estimatedCost = pagestoPrint * cText / black;
                            } else {
                                if (printColor == 0 && lower == "s") {
                                     estimatedCost = pagestoPrint * statement / black;
                            //  } else { 
                                //  System.out.println("oops2");
                                }
                            }
                        }

    //EstimatedCost variable change

    //Cost Estimate
    System.out.printf("%n--- Cost Estimate ---%nInk Usage Per Page: ");
    System.out.print( estimatedCost );
                        }
}
}

我的双倍预估费用不会更新。我的 if else 语句是造成这种情况的原因吗?我一直在寻找,但找不到这段代码有什么问题。我在哪里可以学习如何更好地排除代码故障?有什么建议吗?

最佳答案

Are my if else statements the reason for this?

是的,您的 if 语句写得不好,因为您使用 == 来比较字符串。应使用 equals 方法比较字符串。

例如此检查

lower == "i"

应替换为

"i".equals(lower); 

注意:对文字进行反向检查可避免在 lower 为 null 的情况下发生 NPE

如上所述更新您的 if 条件并尝试运行您的程序。

关于java - 如何更新和打印我的 "estimatedCost"Double? Java新手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18946200/

相关文章:

java - 通过 FTP 将文件从 Android 上传到服务器?

java - 仅包含图像的 ListView

java - setFocus() 始终为假

java - Android 构建工作区错误

java - 在 eclipse 项目中找不到错误

java - 如何使用Java在Kairos Cloud上注册图像?

java - 正则表达式捕获组在一个组中

java - 选择一个联系人并将其填写到EditText中

java - Selenium session 未创建异常错误

java - 在请求权限之前 Android 应用程序已关闭?怎么解决呢?