开发者社区> 问答> 正文

从键盘(1个值参数)或一个文件(0个值参数)读取的方法在0个参数下无法正常工作

我希望我的方法从键盘或文件中读取三角形,然后将其添加到ArrayList..我有2个类:由2个坐标定义的点:int c1,int c2和由颜色字符串和3个点定义的Triangle类。我有2种方法,一种在Triangle课堂上,一种在Point课堂上:

三角类方法:

public Triangle readingTriangle(int value) throws Exception {
        if(value == 1) {
        Scanner sc = new Scanner(System.in);
        Triangle reserve =new Triangle();
        reserve.color = sc.next();
        reserve.setA(reserve.A.readingPoint(1));
        reserve.setB(reserve.B.readingPoint(1));
        reserve.setC(reserve.C.readingPoint(1));
        return reserve;
        }
        if(value == 0) {
            Scanner input = new Scanner(new File("File"));
            Triangle reserve1 =new Triangle();

            reserve1.color=input.next();
            reserve1.setA(reserve1.A.readingPoint(0));
            reserve1.setB(reserve1.B.readingPoint(0));
            reserve1.setC(reserve1.C.readingPoint(0));
            return reserve1;
        }
        return new Triangle();


    }
    ```
点类方法:

public Point readingPoint(int value) throws Exception { if(value==1) { Scanner sc = new Scanner(System.in); Point reserve=new Point(); reserve.c1 = sc.nextInt(); reserve.c2 = sc.nextInt(); return reserve; } if(value==0) { Point reserve1=new Point(); Scanner input1 = new Scanner(new File("File"));

        input1.next();
        reserve1.c1=input1.nextInt();
        reserve1.c2=input1.nextInt();


        return reserve1;

    }
    return new Point();

}
```

在测试课中:


    Scanner sc = new Scanner(System.in);
    int triangleNumber = sc.nextInt();
    List<Triangle> triangleList = new ArrayList<Triangle>();

    for (int i = 0; i < triangleNumber; i++) {
        Triangle t = new Triangle();
        Triangle t1 = t.readingTriangle(0);
        triangleList.add(t1);

    }
    System.out.println(triangleList);
    
    ```
 
        
我的输出:

[Triangle [color=green, A=Point [c1=20, c2=20], B=Point [c1=20, c2=20], C=Point [c1=20, c2=20]]]

我的档案:

green 20 20 40 40 60 60

首先,我想提到第一种方法正在工作(1个值参数正在工作),我在读取文件时遇到问题。您可以从输出中看到问题:B点和C点给出“ 20 20”。

我有一些我要尊重的东西:

- 我希望我的方法结构能够得到尊重。

- 必须使用扫描仪(我知道这是旧技术);

- 我不想更改我的文件,因为如果它是一个真正的记事本,我认为我不会留下来更改记事本中的内容。

其他观察:

我认为问题出在扫描仪方法上,我尝试了while(input1.hasNext())。
    
    
    
    问题来源:Stack Overflow

展开
收起
montos 2020-03-22 20:19:45 860 0
1 条回答
写回答
取消 提交回答
  • 您必须使用的相同实例Scanner。在这里,您正在循环的每次迭代中创建一个新实例,因此每次它将从文件的开头开始。因此,我建议在主类中创建一个实例,并将其作为参数传递给您的方法。

    三角类方法:

    public Triangle readingTriangle(int value, Scanner sc,Scanner input) throws Exception {
        if(value == 1) {
        Triangle reserve =new Triangle();
        reserve.color = sc.next();
        reserve.setA(reserve.A.readingPoint(1,sc,input));
        reserve.setB(reserve.B.readingPoint(1,sc,input));
        reserve.setC(reserve.C.readingPoint(1,sc,input));
        return reserve;
        }
        if(value == 0) {
            Triangle reserve1 =new Triangle();
    
            reserve1.color=input.next();
            reserve1.setA(reserve1.A.readingPoint(0,sc,input));
            reserve1.setB(reserve1.B.readingPoint(0,sc,input));
            reserve1.setC(reserve1.C.readingPoint(0,sc,input));
            return reserve1;
        }
        return new Triangle();
    }
    

    点类方法:

    public Point readingPoint(int value, Scanner sc,Scanner input) throws Exception {
        if(value==1) {
        Point reserve=new Point();
        reserve.c1 = sc.nextInt();
        reserve.c2 = sc.nextInt();
        return reserve;
        }
        if(value==0) {
            Point reserve1=new Point();
            reserve1.c1=input.nextInt();
            reserve1.c2=input.nextInt();
    
    
            return reserve1;
    
        }
        return new Point();
    
    }
    

    在测试课中:

    Scanner sc = new Scanner(System.in); //the only time you create new instance of Scanner(System.in) -> for keybord input
    Scanner input = new Scanner(new File("File"));//the only time you create new instance of Scanner(new File("File")) -> for file input
    
    int triangleNumber = sc.nextInt();
    List<Triangle> triangleList = new ArrayList<Triangle>();
    for (int i = 0; i < triangleNumber; i++) {
        Triangle t = new Triangle();
        Triangle t1 = t.readingTriangle(0,sc,input);
        triangleList.add(t1);
    
    }
    System.out.println(triangleList);
    

    回答来源:Stack Overflow

    2020-03-22 20:20:45
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载