问题描述
好吧,所以我写了两个蓝图类和一个测试器.我不断遇到这个错误:
Ok so I have written two blueprint classes and one tester.I keep getting this same error:
'java.util.Scanner.throwFor(Unknown Source)'.
有人能启发我做错什么吗?这是我的代码.
Can anyone enlighten me on what I am doing wrong? Here is my code.
蓝图类ShoppingCart:
blueprint class ShoppingCart:
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:
Blueprint 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();
}
}
测试器类:
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();
}
}
推荐答案
问题:
您的问题是您正在使用 scan()
方法关闭 Scanner
.以某种非直觉的方式,关闭扫描程序还会关闭 System.in
输入流,从而使您无法在 billCal()中创建新的
Scanner
方法.
Your problem is that you're closing your Scanner
in the scan()
method. Somewhat non-intuitively, closing the Scanner also closes the System.in
input stream, thus causing you to be unable to create a new Scanner
in the billCal()
method.
解决方案:
您可以 A)删除 scan()
方法末尾的 in.close()
或 B)在主要方法中创建一个 Scanner
,并将其传递给您的每个方法.
You can either A) remove in.close()
at the end of your scan()
method or B) create a single Scanner
in the main method, and pass it to each of your methods.
这篇关于java.util.Scanner.throwFor(未知来源).为什么我的代码不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!