我是 Java 的新手(2 周),基本上我正在嘗試做一個三角形問題。我需要輸入一個看起來像這樣的文本文件:2 2 23 3 23 x 4我可以讓它讀取文件并正確顯示它,但是我需要它顯示“等邊”“等腰線”“不等邊線”或不是三角形,因為......我無法根據來自文本文件。這是我到目前為止所擁有的。 public static void main(String[] args) throws Exception { File file = new File("input.txt"); Scanner sc = new Scanner(file); while (sc.hasNextLine()) System.out.println(sc.nextLine()); }}這基本上沒什么。我知道我需要 3 個數組。有人可以朝正確的方向啟動我嗎?
2 回答

月關寶盒
TA貢獻1772條經驗 獲得超5個贊
您需要設置sc.nextLine()一個變量來使用,而不是立即將其作為輸出打印出來。如果三個數字的集合出現在一行中,您可能希望使用該split()方法,當您理解數組時,該方法非常容易使用。
讓您開始:
public static void main(String[] args) throws Exception
{
File file =
new File("input.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine())
String firstLine = sc.nextLine();
String[] sides = firstLine.split(" ");// Get the numbers in between the spaces
// use the individual side lengths for the first triangle as you need
// Next iteration works through the next triangle.
}
添加回答
舉報
0/150
提交
取消