2014-11-03 42 views
0

好了,所以我已經寫了兩個藍圖類和一個tester.I繼續得到同樣的錯誤:java.util.Scanner.throwFor(未知來源)。爲什麼我的代碼不工作?

'java.util.Scanner.throwFor(Unknown Source)'. 

任何人都可以見識一下什麼,我做錯了什麼?這是我的代碼。

藍圖級的購物:

package exercise3; 

import java.util.Scanner; 
import javax.swing.JOptionPane; 
public class ShoppingCart 
{ 
    ScanShop amount = new ScanShop(); 





    public void getbill() 
    { 

     JOptionPane.showMessageDialog(null,"your total is: " + amount.getcart()); 
    } 

    public void billCal() 
    { 
     String answer; 
     int number; 
     Scanner input = new Scanner(System.in); 

     /*System.out.println("please enter how much your bill is:..."); 
     //how much bill is: 
     cart = in.nextDouble(); 
     in.nextLine(); 
     System.out.printf("you have entered: %.2f", cart);*/ 

     System.out.println("Do you have a loyalty card? y or n"); 
     // asking do you have loyalty card 
     answer= input.next(); 


     if (answer.equalsIgnoreCase("y")) 
     { 

      amount.setcart(amount.getcart()*0.9); 

      //other vouchers to discount 
      System.out.println("thats great! do you have a voucher: " 
        + "\n(1) £5 " 
        + "\n(2) £10 " 
        + "\n (3) no vouchers"); 
      number= input.nextInt(); 
      switch(number) 
      { 
      case 1 : 
       amount.setcart(amount.getcart()-5); 
       getbill(); 
       break; 

      case 2 : 
       amount.setcart(amount.getcart()-10); 
       getbill(); 
       break; 

      default : 
       getbill(); 
       break; 
      } 
     } 


     else 
     { 
      getbill(); 
     } 


     input.close(); 
    }//closing billCal 

} 

藍圖ScanShop:

package exercise3; 
import java.util.Scanner; 
public class ScanShop 
{ 
private double cart; 

public double getcart() 
{ 
    return cart; 
} 
public void setcart(double cart) 
{ 
    this.cart =cart; 
} 
public void scan() 
{ 

    //the prices of items 
    double p1; 
    double p2; 
    double p3; 
    double total; 

    Scanner in = new Scanner(System.in); 

    System.out.println("what price is item one?"); 
    p1 = in.nextDouble(); 
    System.out.println("What is price of item two?"); 
    p2= in.nextDouble(); 
    System.out.println("And what is the price of the third item?"); 
    p3= in.nextDouble(); 
    in.nextLine(); 

     total = p1 + p2 + p3; 


    System.out.printf("The total bill is %.2f\n\n", total); 


    setcart(total); 

    System.out.println("the cart is: " + getcart()); 

    in.close(); 




} 
} 

Tester類:

package exercise3; 

public class ShoppingCart_Test { 
public static void main (String [] arg){ 
    ShoppingCart customerOne = new ShoppingCart(); 
    //c1 is customer one 
    ScanShop c1 = new ScanShop(); 


    c1.scan(); 
    customerOne.billCal(); 


} 
} 

回答

0

問題:

您的問題是您正在通過scan()方法關閉您的Scanner。有點不直觀的是,關閉掃描器也會關閉System.in輸入流,從而導致您無法在billCal()方法中創建新的Scanner

解決方案:

您可以A)在您的scan()方法或B的端部除去in.close()創建在主方法的單個Scanner,並將它傳遞到您的每個方法。

+0

OMG謝謝你這麼多。 – Crazypigs 2014-11-05 11:44:16

+0

如果有人在之前的代碼中使用了'.close',你如何打開'system.in輸入'流。像這樣的人不應該關閉掃描器,這樣如果有人正在更新程序,可以在沒有這種情況下製作另一臺掃描器。 – Crazypigs 2014-11-05 11:50:42

+0

@crazypigs我不認爲有可能重新打開System.in。我不知道這是爲什麼,但是哦... – Azar 2014-11-05 19:56:51

相關問題