创建一个Java程序,读取.txt并识别三角形边长

创建一个Java程序,读取.txt并识别三角形边长,java,input,output,Java,Input,Output,我试图创建一个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

我试图创建一个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){
}
下面是您如何接受输入(可能的方法之一)

  • 检查输入流中是否有更多行
  • 如果有更多行,请阅读下一行
  • 标记下一行(提取每个标记)
  • 您希望每行正好有3个整数
  • 因此,首先检查下一行中是否正好有3个令牌。如果没有,请相应处理
  • 如果正好有3个标记,请尝试将每个标记转换为整数
  • 如果其中任何一个不能转换为整数,请相应地进行处理
  • 如果所有3个标记都成功地转换为整数,那么就得到了三角形的3条边。现在您可以检查它是否是三角形,以及它是什么类型的三角形

如果您需要一些解释,请向老板致谢,这很有帮助。以前没有使用过integer.parseInt,但是在那里查找我没有看到的部分,然后将它们组合在一起。再次感谢。
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)
    }
}
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;
    String str[];
    int s1=0,s2=0,s3=0;
try 
{ 
    Scanner file = new Scanner( new File("input.txt"));
    String line;
    while((line=file.nextLine())!=null)
    {
        str = line.split(" ");

        if(str.length==3)
        {
            try
            {
                s1 = Integer.parseInt(str[0]);
                s2 = Integer.parseInt(str[1]);
                s3 = Integer.parseInt(str[2]);

                if(s1+s2>=s3 && s2+s3>=s1 && s1+s3>=s2)
                {
                    if(s1==s2 && s2==s3) System.out.println("Equilateral");
                    else if(s1==s2 || s2==s3 || s3==s1) System.out.println("Isosceles");
                    else System.out.println("Scalene");
                }
                else
                {
                    System.out.println("Not a Triangle");
                }
            }
            catch(Exception e)
            {
                System.out.println("Not a Triangle");
            }

        }
        else
        {
            System.out.println("Not a Triangle");
        }
    }
}
catch(Exception e)
{

}

}

}