2017-01-05 51 views
-1

我正在嘗試創建貨幣轉換器。我的代碼中有一個貨幣表作爲鏈接彈出。我想問用戶他們是否要轉換爲美元。我認爲放置括號是錯誤的。我希望如果有人能看節目,讓我知道他們想什麼:)謝謝你的幫助簡單的Java貨幣轉換器錯誤

package Testing; 

import java.awt.Color; 
import java.awt.Desktop; 
import java.awt.Font; 
import java.util.Scanner; 

import javax.swing.JEditorPane; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 
import javax.swing.event.HyperlinkEvent; 
import javax.swing.event.HyperlinkListener; 

public class Test1 { 
    public static JFrame f = new JFrame("HyperlinkListener"); 

    public static void main(final String[] args) { 

     Scanner stdin = new Scanner(System.in); // creates new scanner 

     System.out.println("Hello, welcome to currency convertor"); //intro 
     System.out.println(); 
     System.out.println("You will be able to convert from, and to, the U.S dollar"); //asks user what to think of what they want to convert 
     System.out.println("A box is going to appear, please click it and find your specific exchange rate. "); // has user go to exchange rate table 

     SwingUtilities.invokeLater(new Runnable() { //opens a text box that allows the user to click on a link that brings them to the website 
      @Override 
      public void run() { //runs the window to open link 
       Font font = new Font("Serif", Font.BOLD, 12); //picks font size and font type 
       JEditorPane jep = new JEditorPane(); 
       jep.setContentType("text/html"); //set content as html 
       jep.setFont(font); 
       jep.setText(
         "Click <a href='http://www.x-rates.com/table/?from=USD&amount=1'> this button </a> to see the table."); //the text that will be displayed 
       jep.setEditable(false); //to ensure the user can't type in the box 
       jep.setOpaque(true); // to ensure the box isn't see through 
       jep.setBackground(Color.RED); // change box color to red 
       jep.setSize(100, 500); //set size of link box 
       jep.addHyperlinkListener(new HyperlinkListener() { 
        @Override 
        public void hyperlinkUpdate(final HyperlinkEvent hle) { 
         if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) { 
          System.out.println(hle.getURL()); 
          Desktop desktop = Desktop.getDesktop(); 
          try { 
           desktop.browse(hle.getURL().toURI()); 
          } catch (Exception ex) { 
           ex.printStackTrace(); 
          } 
         } 
        } 
       }); 

       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       f.add(jep); 
       f.pack(); 
       f.setLocation(400, 200); //says where on the screen the box will be 
       f.setVisible(true); 
      } 
     }); 

     boolean yes = true; 

     double exchangerate, currency, currencyfrom, end; 

     System.out.println("If you are converting to the U.S dollar, say true. If not, say false"); 
     { 
      if (true) { 
       boolean correct = true; 
       do { //while loop, so the user can repeat 
        System.out.println("What is the currency you're converting from"); 
        currency = stdin.nextDouble(); 
        System.out.println("How many?"); 
        currencyfrom = stdin.nextDouble(); 
        System.out.println("What is the exchange rate?"); 
        exchangerate = stdin.nextDouble(); 
        end = currencyfrom/exchangerate; 
        System.out.printf("You have %d %d", end, currency); 
        System.out.println("Do you want to do another, say true if you do, say false if not"); 
        yes = stdin.nextBoolean(); //if they say true, it will go through again 
       } while (yes); 
       System.out.println("Thank you for using the currency converter!"); //prints out when the user is done 

      } 
      { 
       if (false) { 
        do { //while loop, so the user can repeat 

         System.out.println("What is the currency you're converting to"); 
         currency = stdin.nextDouble(); 
         System.out.println("How many?"); 
         currencyfrom = stdin.nextDouble(); 
         System.out.println("What is the exchange rate?"); 
         exchangerate = stdin.nextDouble(); 
         end = currencyfrom * exchangerate; 
         System.out.printf("You have %d %d", end, currency); 
         System.out.println("Do you want to do another, say true if you do, say false if not"); 
         yes = stdin.nextBoolean(); //if they say true, it will go through again 
        } while (correct); 
       } 
       System.out.println("Thank you for using the currency converter!"); //prints out when the user is done      

      } 
     } 
    } 
} 
+3

歡迎來到Stack Overflow!請參閱[如何提問](http://stackoverflow.com/help/how-to-ask)和[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – radoh

+0

你的「正確」變量是什麼? – DimaSan

回答

0

correct變量內聲明的是,如果(真)塊和它的範圍是有限的到這個塊,do-while(正確的)塊是未知的。

只需在這些塊之外移動此聲明,就像您對變量yes所做的一樣。

boolean yes = true; 
boolean correct = true; 

double exchangerate, currency, currencyfrom, end; 
....