我正在尝试创建一个Java程序,它从文本文件中读取一行中的三个数字,并将它们解释为三角形三边的长度,打印出来:
"Equilateral" - if the three lines are equal length
"Isosceles" - if only two of the three lines are equal
"Scalene" - for any other valid length
...or "Not a Triangle"
我的文字文件看起来像这样......
1 1 1
2 2 1
2 x 3
到目前为止,我已经拥有了这个,并且不知道从哪里可以获得任何帮助,非常感谢!!
import java.util.Scanner;
import java.io.File;
import java.util.NoSuchElementException;
import java.io.FileNotFoundException;
public class Triangle {
public static void main (String[] args){
File fileName = new File("input.txt");
Scanner scan = null;
try {
Scanner file = new Scanner( new File("input.txt");
}
while(scan.hasNextLine()) {
}
catch(FileNotFoundException){
}
答案 0 :(得分:0)
while(scan.hasNextLine()) {
String[] line = (scan.nextLine()).split("\\s+");
if(line.length != 3) {
// the next line doesn't have exactly 3 integers, do something
}
try {
int side1 = Integer.parseInt(line[0]);
int side2 = Integer.parseInt(line[1]);
int side3 = Integer.parseInt(line[2]);
// check if it is a triangle, and what kind of triangle is it
}
catch(NumberFormatException e) {
// there is something else in the line, other than integer value(s)
}
}
以下是如何接受输入(可能的方式之一)。
答案 1 :(得分:0)
module.export {
port :8000, // to any available port
...
}