2017-01-05 2 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

Добро пожаловать в переполнение стека! См. [Как спросить] (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; 
.... 
Смежные вопросы