尝试并发现问题

时间:2015-02-23 02:24:08

标签: java

我试图在这个例子中写一个'try and catch'方法,但由于某些原因,我在所有变量上都出错了; “找不到标志”。这种情况发生在以下所有情况中: 小计& 客户类型。有谁知道是什么原因引起的?

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

public class InvoiceApp
{
public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    String choice = "y";

    while (!choice.equalsIgnoreCase("n"))
    {
        // get the input from the user
        try
        {    
        //System.out.print("Enter customer type (r/c): ");
            String customerType = getValidCustomerType(sc);
            System.out.print("Enter subtotal:   ");
            double subtotal = sc.nextDouble();
        }
        catch (InputMismatchException e)
        {
            sc.next();
            System.out.println("Error! Invalid number. Try again \n");
            continue;
        }

        // get the discount percent
        double discountPercent = 0;
        if (customerType.equalsIgnoreCase("R"))
        {
            if (subtotal < 100)
                discountPercent = 0;
            else if (subtotal >= 100 && subtotal < 250)
                discountPercent = .1;
            else if (subtotal >= 250)
                discountPercent = .2;
        }
        else if (customerType.equalsIgnoreCase("C"))
        {
            if (subtotal < 250)
                discountPercent = .2;
            else
                discountPercent = .3;
        }
        else
        {
            discountPercent = .1;
        }

        // calculate the discount amount and total
        double discountAmount = subtotal * discountPercent;
        double total = subtotal - discountAmount;

        // format and display the results
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        NumberFormat percent = NumberFormat.getPercentInstance();
        System.out.println(
                "Discount percent: " + percent.format(discountPercent) + "\n" +
                "Discount amount:  " + currency.format(discountAmount) + "\n" +
                "Total:            " + currency.format(total) + "\n");

        // see if the user wants to continue
        System.out.print("Continue? (y/n): ");
        choice = sc.next();
        System.out.println();
    }
}

4 个答案:

答案 0 :(得分:1)

您在try块中声明subtotalcustomerType,以便该变量仅在try块中可见。

像这样更改你的代码将解决问题:

double subtotal = 0;
String customerType = "";
try
{    
        //System.out.print("Enter customer type (r/c): ");
        String customerType = getValidCustomerType(sc);
        System.out.print("Enter subtotal:   ");
        subtotal = sc.nextDouble();
}
catch (InputMismatchException e)
{
        sc.next();
        System.out.println("Error! Invalid number. Try again \n");
        continue;
}

更多:Blocks and Statements

答案 1 :(得分:1)

代码的问题在于变量的范围:如果在try块中声明某些内容,则只能在try块中显示;它不会在try块之外显示,甚至包括它之后的catch块。

为了解决这个问题,请在try块之外声明变量:

String customerType;
double subtotal;
try {    
//System.out.print("Enter customer type (r/c): ");
    customerType = getValidCustomerType(sc);
    System.out.print("Enter subtotal:   ");
    subtotal = sc.nextDouble();
} catch (InputMismatchException e) {
    sc.next();
    System.out.println("Error! Invalid number. Try again \n");
    continue;
}

答案 2 :(得分:0)

您在try {}的括号内声明了subtotal和customertype。这是他们唯一有效的地方。如果你在尝试之前宣布它们就没问题了。

答案 3 :(得分:0)

    private static String getValidCustomerType(Scanner sc)
{
    String customerType = "";
    boolean isValid = false;
    while (isValid == false)
    {
        System.out.print("Enter customer type (r/c): ");
        customerType = sc.next();
        if  (customerType.equalsIgnoreCase("r") || (customerType.equalsIgnoreCase("c")))
        isValid = true;            
       else
        {
            System.out.println("Invalid customer type. Try again. \n");
        }    
        sc.nextLine();
    }
    return customerType;
}
}