Perhaps the problem with the debug error was not initialized by the error

I saw a lot of similar questions in the archives, but I can not find such a scenario as the problem I am facing.

Below is my code. I ran into errors with "finalPrice" and "grandTotalPrice" might not have been initialized. The lines of code are at the end of the program.

Variables must be assigned totals through console input at the top. I am not sure what the error is or why. Can anyone help me and explain?

the code:

import java.util.*;


public class PictureFrames
{

    static Scanner console = new Scanner(System.in);

    static final double REGULAR_FRAME = .15, FANCY_FRAME = .25;
    static final double COLOR = .10, CARDBOARD = .02, GLASS = .07, CROWNS = .35;


    public static void main (String[] args)
    {

    double length, width, area, perimeter; 
    double priceOfFrame, priceOfColor, priceOfCardboard, priceOfGlass, priceOfCrowns, finalPrice, crownFinalPrice, grandTotalPrice; 
    int numberOfCrowns;
    char typeOfFrame, choiceOfColor, choiceOfCrowns;



    System.out.println ("Please enter the length of your picure in inches:");
    length = console.nextDouble();

    System.out.println ("Please enter the width of your picure in inches: ");
    width = console.nextDouble();

    System.out.println ("Please enter the type of frame: R or r (Regular), F or f (Fancy). ");  
    typeOfFrame = console.next().charAt(0);

    System.out.println ("Would you like to add color?: Y for (Yes), N for (No): "); 
    choiceOfColor = console.next().charAt(0);


    switch (typeOfFrame)
    {
    case 'R':
    case 'r':
        if (choiceOfColor == 'N')
        {
            area = (length * width);
            perimeter = (2 * length) + (2 * width);
            priceOfFrame = (perimeter * REGULAR_FRAME);
            priceOfCardboard = (area * CARDBOARD);
            priceOfGlass = (area * GLASS);
            finalPrice = (priceOfFrame + priceOfCardboard + priceOfGlass);
        break;
        }
        else if (choiceOfColor == 'Y')
        {
            area = (length * width);
            perimeter = (2 * length) + (2 * width);
            priceOfColor = (area * COLOR);          
            priceOfFrame = (perimeter * REGULAR_FRAME);
            priceOfCardboard = (area * CARDBOARD);
            priceOfGlass = (area * GLASS);      
            finalPrice = (priceOfFrame + priceOfColor + priceOfCardboard + priceOfGlass);       
        break;
        }
    case 'F':
    case 'f':
        if (choiceOfColor == 'N')
        {
            area = (length * width);
            perimeter = (2 * length) + (2 * width);
            priceOfFrame = (perimeter * FANCY_FRAME);
            priceOfCardboard = (area * CARDBOARD);
            priceOfGlass = (area * GLASS);
            finalPrice = (priceOfFrame + priceOfCardboard + priceOfGlass);
        break;
        }
        else if (choiceOfColor == 'Y')
        {
            area = (length * width);
            perimeter = (2 * length) + (2 * width);
            priceOfColor = (area * COLOR);          
            priceOfFrame = (perimeter * FANCY_FRAME);
            priceOfCardboard = (area * CARDBOARD);
            priceOfGlass = (area * GLASS);      
            finalPrice = (priceOfFrame + priceOfColor + priceOfCardboard + priceOfGlass);       
        break;
        }

}       

    System.out.println ("Would you like to add crowns? Enter Y (Yes), or N (No): ");    
    choiceOfCrowns = console.next().charAt(0);

    if (choiceOfCrowns == 'Y')
    {
        System.out.println ("How many crowns would you like? ");    
        numberOfCrowns = console.nextInt();     
        crownFinalPrice =(numberOfCrowns * CROWNS);
        grandTotalPrice = (crownFinalPrice + finalPrice);
    }   
    else if (choiceOfCrowns == 'N')
            System.out.printf ("Your total comes to: $%.2f%n", grandTotalPrice);    

    }   

}
+5
source share
3 answers

, , . , :

  • ( ) , . , ( ) . , - / , .

  • main() . , , " ", ... , . , , ?

  • . - . .. , .. SO. , Java 45: .

  • , . final , . , , - , , , . /, , , , . , . . SO. , Java. 15: Mutability.

  • enum - String char - . , , , . , ( , ) , / , . , , / . , .

  • - . , , . SO: " ...?" , - , area = length x width, . .

  • Exception . . , , - , . , , . , , , .

, , , , . :

import java.util.*;

public final class PictureFrames
{

  static Scanner console = new Scanner(System.in);

  static final double REGULAR_FRAME = .15, FANCY_FRAME = .25;
  static final double COLOR = .10, CARDBOARD = .02, GLASS = .07, CROWNS = .35;

  enum FrameType {
    /** Regular. */
    R, 
    /** Fancy. */
    F;
  };


  static double areaPriceInDollars(final FrameType frameType,
                                   final double length,
                                   final double width,
                                   final boolean color)
  {
    final double area,perimeter,
      priceOfFrame,
      priceOfCardboard,
      priceOfGlass,
      priceOfColor;

      area = length * width;
      perimeter = 2 * (length + width);

      priceOfCardboard = (area * CARDBOARD);
      priceOfGlass = (area * GLASS);      

      if (color) 
        priceOfColor = area * COLOR;
      else 
        priceOfColor = 0.0;

      switch (frameType) {
        case R:
          priceOfFrame = (perimeter * REGULAR_FRAME);
          break;
        case F:
          priceOfFrame = (perimeter * FANCY_FRAME);
          break;
        default:
          throw new IllegalArgumentException("FrameType "+frameType+" unknown, no price available.");
      }

      return priceOfColor + priceOfCardboard + priceOfGlass + priceOfFrame;
    }       


  public static void main(String[] args)
  {
    System.out.println("Please enter the length of your picure in inches:");
    final double length = console.nextDouble();

    System.out.println("Please enter the width of your picure in inches: ");
    final double width = console.nextDouble();

    System.out
        .println("Please enter the type of frame: R or r (Regular), F or f (Fancy). ");
    final char typeOfFrame = console.next().charAt(0);
    FrameType frameType = FrameType.valueOf(""
        + Character.toUpperCase(typeOfFrame));

    System.out
        .println("Would you like to add color?: Y for (Yes), N for (No): ");
    final char choiceOfColor = console.next().charAt(0);
    final boolean color = Character.toUpperCase(choiceOfColor) == 'Y';

    System.out
        .println("Would you like to add crowns? Enter Y (Yes), or N (No): ");
    final char choiceOfCrowns = console.next().charAt(0);
    final boolean crowns = Character.toUpperCase(choiceOfCrowns) == 'Y';

    final double priceOfCrowns;
    if (crowns) {
      System.out.println("How many crowns would you like? ");
      final int numberOfCrowns = console.nextInt();
      priceOfCrowns = (numberOfCrowns * CROWNS);
    } else {
      priceOfCrowns = 0.0;
    }

    final double grandTotalPrice = priceOfCrowns
        + areaPriceInDollars(frameType, length, width, color);
    System.out.printf("Your total comes to: $%.2f%n", grandTotalPrice);
  }
}
+5

, typeOfFrame R, r, F f? , "" .

, , ( ). , ( isValidChoice isFrameType , ). , , - , , , Y N , Q. , ( printf).

, , ( ), . java.math.BigDecimal.

class Main
{
    static Scanner console = new Scanner(System.in);    
    static final double REGULAR_FRAME = .15;
    static final double FANCY_FRAME = .25;
    static final double COLOR = .10;
    static final double CARDBOARD = .02;
    static final double GLASS = .07;
    static final double CROWNS = .35;

    public static void main (String[] args)
    {   
        final double length;
        final double width;
        final char   typeOfFrame;
        final char   choiceOfColor;

        System.out.println ("Please enter the length of your picure in inches:");
        length = console.nextDouble();

        System.out.println ("Please enter the width of your picure in inches: ");
        width = console.nextDouble();

        System.out.println ("Please enter the type of frame: R or r (Regular), F or f (Fancy). ");  
        typeOfFrame = console.next().charAt(0);

        System.out.println ("Would you like to add color?: Y for (Yes), N for (No): "); 
        choiceOfColor = console.next().charAt(0);

        if(!(isFrameType(typeOfFrame)))
        {

        }
        else
        {
            final double area;
            final double perimeter; 
            final double priceOfFrame;
            final double priceOfCardboard;
            final double priceOfGlass;

            area             = (length * width);
            perimeter        = (2 * length) + (2 * width);
            priceOfFrame     = (perimeter * REGULAR_FRAME);
            priceOfCardboard = (area * CARDBOARD);
            priceOfGlass     = (area * GLASS);  

            if(isValidChoice(choiceOfColor))
            {
                final double priceOfColor;
                final double finalPrice;
                final char   choiceOfCrowns;
                final double grandTotalPrice; 

                if(choiceOfColor == 'N')
                {
                    finalPrice = (priceOfFrame + priceOfCardboard + priceOfGlass);
                }
                else
                {
                    priceOfColor = (area * COLOR);          
                    finalPrice   = (priceOfFrame + priceOfColor + priceOfCardboard + priceOfGlass);     
                }    

                System.out.println ("Would you like to add crowns? Enter Y (Yes), or N (No): ");    
                choiceOfCrowns = console.next().charAt(0);

                if(isValidChoice(choiceOfCrowns))
                {
                    if(choiceOfCrowns == 'Y')
                    {
                        final double crownFinalPrice;
                        final int    numberOfCrowns;

                        System.out.println ("How many crowns would you like? ");    
                        numberOfCrowns  = console.nextInt();        
                        crownFinalPrice =(numberOfCrowns * CROWNS);
                        grandTotalPrice = (crownFinalPrice + finalPrice);
                    }   
                    else
                    {
                        grandTotalPrice = finalPrice;
                    }

                    System.out.printf ("Your total comes to: $%.2f%n", grandTotalPrice);    
                }
            }
        }
    }   

    private static boolean isFrameType(final char c)
    {        
        final char lower;

        lower = Character.toLowerCase(c);

        return (lower == 'r' || lower == 'f');
    }

    private static boolean isValidChoice(final char c)
    {
        return (c == 'Y' || c == 'N');
    }
}
+4

, , , , . switch if, , .

, , :

double finalPrice = 0, grandTotalPrice = 0; 

but you can also make sure that they are installed no matter which path the application takes (it’s known that all possible paths are good practice).

0
source

All Articles