[英]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)
{
}
}
}
I am getting a duplicate local variable at the line我在该行得到一个重复的局部变量
" Scanner file = new Scanner( new File("c:/eclipse/numbers"));" “扫描仪文件=新扫描仪(新文件(“c:/eclipse/numbers”));”
I am a noob to this, and honestly cannot figure it out.我对此是个菜鸟,老实说,我无法弄清楚。 Any advice/input would be great.
任何建议/意见都会很棒。
You defined File file
earlier, so that name is taken when you define Scanner file
.您之前定义了
File file
,因此在定义Scanner file
时会使用该名称。 Change one of them and everywhere you reference it.更改其中一个,并在您引用它的任何地方进行更改。
Note that a try
block doesn't have its own local variables;请注意,
try
块没有自己的局部变量。 it shares those of the parent section.它与父部分共享。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.