2015-02-23 103 views
1

我想在這個例子中寫一個'try and catch'方法,但由於某些原因,我在所有變量中出現錯誤; 「找不到標誌」。這發生在所有情況下: 小計& customerType。有誰知道可能是什麼原因造成的?嘗試並發現問題

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(); 
    } 
} 

回答

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; 
} 
+0

現在告訴我:customerType = getValidCustomerType無效。 我是否需要設置另一個靜態方法以使try和catch正常工作? – 2015-02-23 02:32:55

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

+0

我忘了說,這是在代碼中還有: – 2015-02-23 02:29:59

+0

我不想成爲一個堅持己見的人,但你的代碼沒有解決這個問題,我可以「有用」,但對方的回答是什麼解決問題。 – 2015-02-23 02:39:10

0

你宣佈小計和customertype的嘗試的括號內{}。那是他們唯一有效的地方。如果你在嘗試之前宣佈他們,這將會很好。

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; 
} 
}