我希望我的方法从键盘或文件中读取三角形,然后将其添加到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
您必须使用的相同实例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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。