我正在尝试创建货币转换器。我在我的代码中弹出一个货币表作为链接。我想询问用户是否要兑换成美元。我认为括号的放置有误。我很乐意,如果有人能看到这个节目,让我知道他们的想法:)谢谢你的帮助
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
}
}
}
}
答案 0 :(得分:0)
您的correct
变量是在 if(true)块中声明的,其范围仅限于此块, do-while(正确)< / em>阻止。
只需将此声明移到这些块之外,就像使用yes
变量一样。
boolean yes = true;
boolean correct = true;
double exchangerate, currency, currencyfrom, end;
....