2015-11-05 51 views
1

我的最終目標是讓用戶輸入三名選手的名字以及他們完成比賽的時間。它將根據無暇的時間組織選手,並且我想將結果打印到一張表格中,但是當我最後編譯時出現以下錯誤;爲什麼我用JFrame獲得變量聲明錯誤?

error: variable declaration not allowed here 
     JFrame frame = new JFrame(); 

當我單獨運行的JFrame代碼,它工作得很好,但後來當我編譯所有的代碼放在一起我收到上述錯誤的。

import java.awt.BorderLayout; 
import javax.swing.*; 

public class JTableCreatingDemo { 
    public static void main(String args[]) { 

    String runner1, runner2, runner3; 
    double runTime1, runTime2, runTime3; 

    //Get the names and run time of each runner 
    runner1 = JOptionPane.showInputDialog("Enter name of runner 1"); //runner1 
    runTime1 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 1 in minutes")); //runTime1 
    while (runTime1<0) 
     { 
      JOptionPane.showMessageDialog(null, "Invalid Entry"); 
      runtTime1 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 1 in minutes \nEntery format should be MM.SS")); 
     } 

    runner2 = JOptionPane.showInputDialog("Enter name of runner 2"); //runner2 
    runTime2 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 2 in minutes")); //runTime2 
    while (runTime2<0) 
     { 
      JOptionPane.showMessageDialog(null, "Invalid Entry"); 
      runtTime2 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 2 in minutes \nEntery format should be MM.SS")); 
     } 

    runner3 = JOptionPane.showInputDialog("Enter name of runner 3"); //runner3 
    runtime3 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 3 in minutes")); //runTime3 
    while (runTime3<0) 
     { 
      JOptionPane.showMessageDialog(null, "Invalid Entry"); 
      runtTime3 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 3 in minutes \nEntery format should be MM.SS")); 
     } 

    //Sort the runners by their respective run times 
    if(runtime1<=runtime2 && runtime1<=runtime3) //runner1 is the fastest 
    { 
     if(runtime2<=runtime3) 
      JFrame frame = new JFrame(); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      Object rowData[][] = { { runner1, runtime1 + " minutes", "-" }, 
       { runner2, runtime2 + " minutes", (runtime2-runtime1) }, 
       {runner3, runtime3 + " minutes", (runtime3-runTime1)} }; 
      Object columnNames[] = { "Runners Name", "Finish Time", "Differnce" }; 
      JTable table = new JTable(rowData, columnNames); 

      JScrollPane scrollPane = new JScrollPane(table); 
      frame.add(scrollPane, BorderLayout.CENTER); 
      frame.setSize(300, 150); 
      frame.setVisible(true); 
    } 

    } 
} 
+1

你上次'if'忘記'{''}你了嗎? –

+1

您忘記在if語句後面加上括號 –

+0

謝謝!有時我深入研究可能會導致問題的一些問題,從而忘記了基本知識! –

回答

0

試試這個代碼,它應該工作。我剛剛修復了括號內的錯誤:)

import java.awt.BorderLayout; 
import javax.swing.*; 

public class JTableCreatingDemo { 
    public static void main(String args[]) { 

    String runner1, runner2, runner3; 
    double runTime1, runTime2, runTime3; 

    //Get the names and run time of each runner 
    runner1 = JOptionPane.showInputDialog("Enter name of runner 1"); //runner1 
    runTime1 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 1 in minutes")); //runTime1 
    while (runTime1<0) 
     { 
      JOptionPane.showMessageDialog(null, "Invalid Entry"); 
      runtTime1 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 1 in minutes \nEntery format should be MM.SS")); 
     } 

    runner2 = JOptionPane.showInputDialog("Enter name of runner 2"); //runner2 
    runTime2 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 2 in minutes")); //runTime2 
    while (runTime2<0) 
     { 
      JOptionPane.showMessageDialog(null, "Invalid Entry"); 
      runtTime2 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 2 in minutes \nEntery format should be MM.SS")); 
     } 

    runner3 = JOptionPane.showInputDialog("Enter name of runner 3"); //runner3 
    runtime3 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 3 in minutes")); //runTime3 
    while (runTime3<0) 
     { 
      JOptionPane.showMessageDialog(null, "Invalid Entry"); 
      runtTime3 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 3 in minutes \nEntery format should be MM.SS")); 
     } 

    //Sort the runners by their respective run times 
    if(runtime1<=runtime2 && runtime1<=runtime3) //runner1 is the fastest 
    { 
     if(runtime2<=runtime3) 
      { 
      JFrame frame = new JFrame(); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      Object rowData[][] = { { runner1, runtime1 + " minutes", "-" }, 
       { runner2, runtime2 + " minutes", (runtime2-runtime1) }, 
       {runner3, runtime3 + " minutes", (runtime3-runTime1)} }; 
      Object columnNames[] = { "Runners Name", "Finish Time", "Differnce" 
      } 
}; 
      JTable table = new JTable(rowData, columnNames); 

      JScrollPane scrollPane = new JScrollPane(table); 
      frame.add(scrollPane, BorderLayout.CENTER); 
      frame.setSize(300, 150); 
      frame.setVisible(true); 
    } 

    } 
    } 
相關問題