关于数据输入的问题,如何解决?

为什么在 main 方法中输入数据可以完整输出结果,但在定义 的fun() 方法里输入数据不能完整输出结果?是因为不能定义两个Scanner吗?还是因为什么?望详解!

``
import java.util.Scanner;

class Shop{

private String name;
private float price;
private int acount;


public  Shop(String name, float price, int acount) {
    super();
    this.name = name;
    this.price = price;
    this.acount = acount;
}


public String getName() {
    return name;
}


public void setName(String name) {
    this.name = name;
}


public float getPrice() {
    return price;
}


public void setPrice(float price) {
    this.price = price;
}


public int getAcount() {
    return acount;
}


public void setAcount(int acount) {
    this.acount = acount;
}


public void show(float sum){
    System.out.println("总计:" + sum);
}

}

class addGoods extends Shop{

public addGoods(String name,float price,int acount) {
    
    super(name, price, acount);
    
    float sum = price*acount ;
    System.out.println("商品名称:" + name + ",价格:" + price + ",数量:" + acount);
    super.show(sum);
}

}
public class oj1375 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    while(sc.hasNext()){
        
        int n = sc.nextInt();

        for(int i=0; i<n; i++){
            
        
            addGoods dg = new addGoods(sc.next(),sc.nextFloat(),sc.nextInt());
            
        }
    }
    sc.close();
}

}




import java.util.Scanner;

class Shop{

private String name;
private float price;
private int acount;


public  Shop(String name, float price, int acount) {
    super();
    this.name = name;
    this.price = price;
    this.acount = acount;
}


public String getName() {
    return name;
}


public void setName(String name) {
    this.name = name;
}


public float getPrice() {
    return price;
}


public void setPrice(float price) {
    this.price = price;
}


public int getAcount() {
    return acount;
}


public void setAcount(int acount) {
    this.acount = acount;
}


public void show(float sum){
    System.out.println("总计:" + sum);
}

}

class addGoods extends Shop{

public addGoods(String name,float price,int acount) {
    
    super(name, price, acount);
    
    float sum = price*acount ;
    System.out.println("商品名称:" + name + ",价格:" + price + ",数量:" + acount);
    super.show(sum);
}

}
public class pra3 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    while(sc.hasNext()){
        
        int n = sc.nextInt();
        fun(n);
        
    }
    sc.close();
}
public static void fun(int n){
    
    Scanner sc2 = new Scanner(System.in);
    
    for(int i=0; i<n; i++){
        
        while(!sc2.hasNext()){
            
            Thread.yield();
        }
        addGoods dg = new addGoods(sc2.next(),sc2.nextFloat(),sc2.nextInt());
        
    }
    sc2.close();
}

}



![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/957225094976169.png "#left")

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/308345094976198.png "#left")

把你的fun方法中的 sc2.close(); 去掉就好了,Scanner 如果输入的是System.in 是不需要手动去关闭的, 程序结束流自动就释放了

img

img