0
//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 语句是这个原因吗?我一直在寻找,我找不到这段代码有什么问题。我在哪里可以学习更好地对代码进行故障排除?有小费吗?

4

2 回答 2

4

我的 if else 语句是这个原因吗?

是的,您的if陈述写得不好,因为您正在使用==比较字符串。字符串应该使用equals方法进行比较。

例如这个检查

lower == "i"

应该替换为

"i".equals(lower); 

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

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

于 2013-09-22T16:43:11.777 回答
0

我认为仍然存在一些错误,这是一个正确的版本:

//Import scanner
import java.util.Scanner;

public class Main{

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
    pType = pType.toLowerCase();  //Added
    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);


    //Calculation for color printing
    if (printColor == 1){
        if  (PrintType == 't')
                estimatedCost = pagestoPrint * text / color;

        else if (PrintType == 'i')
                 estimatedCost = pagestoPrint * image / color;

        else if  (PrintType == 'c')
                 estimatedCost = pagestoPrint * cText / color;

        else if (PrintType == 's') 
                 estimatedCost = pagestoPrint * statement / color;
    }

    //Calculation for black printing
    else if (printColor == 0){

        if(PrintType == 't')
                 estimatedCost = pagestoPrint * text / black;
        else if (PrintType == 'i')
                 estimatedCost = pagestoPrint * image / black;
        else  if  (PrintType == 'c')
                 estimatedCost = pagestoPrint * cText / black;
        else if (PrintType == 's')
                     estimatedCost = pagestoPrint * statement / black;
    }

    //EstimatedCost variable change

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

您不能将字符串与 == 进行比较,您必须改用 .equals()。使用 == 比较存储在参考中的地址。

您犯的最大错误是比较“lower”而不是“PrintType”。lower 包含“有销售”的答案,您要比较的是印刷类型。所以我改变了它并在printedType上添加了一个toLowerCase()操作。

于 2013-09-22T17:07:13.253 回答