- #1
Nothing000
- 403
- 0
When I compile this program I get the following error message:
Discount.java:119: variable percent might not have been initialized
System.out.print("You'll get a " + percent + " discount on ");
^
WHAT THE HELL AM I DOING WRONG?
Here is the source code:
import java.util.*;
public class Discount
{
public static void main(String[] args)
{
// Declare all named constants.
final String LEVEL3 = "Gold"; // Name of top level
final String PCT3 = "30"; // 30% discount for LEVEL3
final String LEVEL2 = "Silver"; // Name of middle level
final String PCT2 = "20"; // 20% discount for LEVEL2
final String LEVEL1 = "Copper"; // Name of lowest level
final String PCT1 = "10"; // 10% discount for LEVEL1
// Declare a Scanner object for input.
Scanner scan = new Scanner(System.in); // Standard input
// Declare all other required objects.
String
name, // Name of customer
level, // Membership level of customer
percent; // Percent of discount
// Declare all primitive variables.
double
totalPurchases; // Total amount of purchases
// Prompt the user to type in the customer name,
// then read and store the name.
System.out.println("Please enter a customer name: ");
name = scan.nextLine();
// Prompt the user to type in the current member level,
// then read and store the level.
System.out.println("Please enter the customer's member level: ");
level = scan.nextLine();
// Prompt the user to type in the total amount of purchases,
// then read and store that value.
System.out.println("Please enter the amount of purchase: ");
totalPurchases = scan.nextDouble();
// Print the header of the printout including the customer name.
System.out.println("==========================");
System.out.println(" Credit card member service");
System.out.println(" ==========================");
System.out.println("\nCustomer name: " + name);
// Check if the given customer level matches with one of the
// service levels the company provides.
// If the given level does not match with one
// of the given level names, print an error message.
// Otherwise, according to the customer level,
// print out the percentage discount they get.
if (level == LEVEL3)
percent = PCT3;
else if (level == LEVEL2)
percent = PCT2;
else if (level == LEVEL1)
percent = PCT1;
else
{
System.out.println("We're sorry, but we can't recognize" +
"your current member level. \nPlease contact " +
"customer service immediately.");
System.exit(0);
}
System.out.println("\nCongratulations!");
System.out.print("You'll get a " + percent + " discount on ");
System.out.print("your purchases\nduring the Thanksgiving holiday, ");
System.out.println("which totaled " + totalPurchases);
} // End public static void main
} // End public class Discount
Discount.java:119: variable percent might not have been initialized
System.out.print("You'll get a " + percent + " discount on ");
^
WHAT THE HELL AM I DOING WRONG?
Here is the source code:
import java.util.*;
public class Discount
{
public static void main(String[] args)
{
// Declare all named constants.
final String LEVEL3 = "Gold"; // Name of top level
final String PCT3 = "30"; // 30% discount for LEVEL3
final String LEVEL2 = "Silver"; // Name of middle level
final String PCT2 = "20"; // 20% discount for LEVEL2
final String LEVEL1 = "Copper"; // Name of lowest level
final String PCT1 = "10"; // 10% discount for LEVEL1
// Declare a Scanner object for input.
Scanner scan = new Scanner(System.in); // Standard input
// Declare all other required objects.
String
name, // Name of customer
level, // Membership level of customer
percent; // Percent of discount
// Declare all primitive variables.
double
totalPurchases; // Total amount of purchases
// Prompt the user to type in the customer name,
// then read and store the name.
System.out.println("Please enter a customer name: ");
name = scan.nextLine();
// Prompt the user to type in the current member level,
// then read and store the level.
System.out.println("Please enter the customer's member level: ");
level = scan.nextLine();
// Prompt the user to type in the total amount of purchases,
// then read and store that value.
System.out.println("Please enter the amount of purchase: ");
totalPurchases = scan.nextDouble();
// Print the header of the printout including the customer name.
System.out.println("==========================");
System.out.println(" Credit card member service");
System.out.println(" ==========================");
System.out.println("\nCustomer name: " + name);
// Check if the given customer level matches with one of the
// service levels the company provides.
// If the given level does not match with one
// of the given level names, print an error message.
// Otherwise, according to the customer level,
// print out the percentage discount they get.
if (level == LEVEL3)
percent = PCT3;
else if (level == LEVEL2)
percent = PCT2;
else if (level == LEVEL1)
percent = PCT1;
else
{
System.out.println("We're sorry, but we can't recognize" +
"your current member level. \nPlease contact " +
"customer service immediately.");
System.exit(0);
}
System.out.println("\nCongratulations!");
System.out.print("You'll get a " + percent + " discount on ");
System.out.print("your purchases\nduring the Thanksgiving holiday, ");
System.out.println("which totaled " + totalPurchases);
} // End public static void main
} // End public class Discount