索千 2023-03-22 20:52 采纳率: 74.4%
浏览 16

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

为什么在 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")
  • 写回答

2条回答 默认 最新

  • pzzhao 2023-03-22 21:08
    关注

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

    评论

报告相同问题?

问题事件

  • 修改了问题 3月22日
  • 修改了问题 3月22日
  • 创建了问题 3月22日

悬赏问题

  • ¥15 船舶旋回实验matlab
  • ¥30 SQL 数组,游标,递归覆盖原值
  • ¥15 为什么我的数据接收的那么慢呀有没有完整的 hal 库并 代码呀有的话能不能发我一份并且我用 printf 函数显示处理之后的数据,用 debug 就不能运行了呢
  • ¥15 有关于推荐系统jupyter
  • ¥20 gitlab 中文路径,无法下载
  • ¥15 用动态规划算法均分纸牌
  • ¥30 udp socket,bind 0.0.0.0 ,如何自动选取用户访问的服务器IP来回复数据
  • ¥15 关于树的路径求解问题
  • ¥15 yolo在训练时候出现File "D:\yolo\yolov5-7.0\train.py"line 638,in <module>
  • ¥30 戴尔inspiron独显直连