我使用链接列表读取文本文件。链接列表上具有多个元素的索引的产品
20 10 8
4.5 8.45 12.2
8.0 2.5 4.0
1.0 15.0 18.0
3.5 3.5 3.5
6.0 5.0 10.0
每列分别代表长度,宽度和高度。
当我读取文本文件时,每行被视为单个节点。 我想看看有没有办法找到每一行的产品,音量?
如果这是不可能的,那么我需要找到一种方法来从另一个类中创建一个getVolume()方法来单独调用读取的值(l,w,h)。
那么这里是我的代码到目前为止:一如既往的感谢您的帮助!
import java.util.LinkedList;
import java.util.*;
import java.io.*;
public class Runtime2 {
public static void main(String args[])throws Exception {
String content = new String();
int count=0;
File file = new File("dims.txt");
LinkedList<String> list = new LinkedList<String>();
try {
Scanner sc = new Scanner(new FileInputStream(file));
while (sc.hasNextLine()){
content = sc.nextLine();
list.add(content);
}
sc.close();
}
catch (Exception e) {
e.printStackTrace();
System.out.println();
}
Iterator i = list.iterator();
while (i.hasNext()) {
System.out.print("Node " + count++ + ": ");
System.out.println(i.next());
}
public class Box {
double length;
double width;
double height;
Box(double l, double w, double h) {
length=l;
width=w;
height=h;
}
double getVolume() {
double vol=length*width*height;
return vol;
}
boolean isCube() {
if(length==width && width==height) {
System.out.println("The box is a cube.");
}
else
System.out.println("The box is not a cube.");
return true;
}
}