簡體   English   中英

從 a.txt/輸入文件中讀取數字並在 Java Eclipse 中打印一個三角形

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM