[英]Reading numbers from a .txt/input file and printing a triangle in Java Eclipse
package classicTriangle;
import java.io.File;
import java.util.Scanner;
public class classicTriangle {
public static void main(String[] args) {
File file = new File("c:/eclipse/numbers");
Scanner scan = null;
String str[];
int s1=0,s2=0,s3=0;
try
{
Scanner file = new Scanner( new File("c:/eclipse/numbers"));
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)
{
}
}
}
我在该行得到一个重复的局部变量
“扫描仪文件=新扫描仪(新文件(“c:/eclipse/numbers”));”
我对此是个菜鸟,老实说,我无法弄清楚。 任何建议/意见都会很棒。
您之前定义了File file
,因此在定义Scanner file
时会使用该名称。 更改其中一个,并在您引用它的任何地方进行更改。
请注意, try
块没有自己的局部变量。 它与父部分共享。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.