我正在创建一个计算器,并希望有一个分隔数字的按钮,以便我得到十进制数字。我是新手,一直在思考和寻找一周没有进展。我已经读过有关NumberFormat和DecimalFormat的内容,但我无法让它们工作,我坦率地不知道这些是否正确。
import java.awt.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import javax.swing.*;
/** skapar en enkel räknare med det vanligaste räknesätten
*Java Doc kommentarer för utförliga kommentarer för en längre applikation*/
public class CounterApplication extends JFrame implements ActionListener
{
private JTextField t1;
private JButton b1;
private JButton b2;
private JButton bIndex[] = new JButton[24];
private double tal = 0;
private double talTemp = 0;
private String sign;
private JPanel p1;
private JPanel p2;
private JPanel p3;
/** initierar Räknaren som en applet */
public CounterApplication()
{
setLayout(new BorderLayout());
setBackground(Color.lightGray);
p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.add(t1 = new JTextField(22));
t1.setFont(new Font("Sans Serif", Font.BOLD, 12));
t1.setEnabled(false);
t1.setText(""+tal);
p2 = new JPanel();
p2.setLayout(new GridLayout(4,6,4,4));
for(int i = 0; i<24; i++)
{
bIndex[i] = new JButton();
bIndex[i].addActionListener(this);
bIndex[i].setFont(new Font("Sans Serif", Font.BOLD, 16));
bIndex[i].setForeground(Color.blue);
p2.add(bIndex[i]);
}
bIndex[0].setLabel("7");
bIndex[1].setLabel("8");
bIndex[2].setLabel("9");
bIndex[3].setForeground(Color.red);
bIndex[3].setLabel("/");
bIndex[4].setLabel("sqrt");
bIndex[5].setLabel("sin");
bIndex[6].setLabel("4");
bIndex[7].setLabel("5");
bIndex[8].setLabel("6");
bIndex[9].setForeground(Color.red);
bIndex[9].setLabel("*");
bIndex[10].setLabel("%");
bIndex[11].setLabel("cos");
bIndex[12].setLabel("1");
bIndex[13].setLabel("2");
bIndex[14].setLabel("3");
bIndex[15].setForeground(Color.red);
bIndex[15].setLabel("-");
bIndex[16].setLabel("1/x");
bIndex[17].setLabel("log");
bIndex[18].setLabel("0");
bIndex[19].setLabel("+/-");
bIndex[20].setLabel(",");
bIndex[21].setForeground(Color.red);
bIndex[21].setLabel("+");
bIndex[22].setLabel("=");
bIndex[23].setLabel("ln");
p3 = new JPanel();
p3.setLayout(new GridLayout(1,2,4,4));
b1 = new JButton("CE");
b1.setFont(new Font("Sans Serif", Font.BOLD, 16));
b1.setForeground(Color.red);
p3.add(b1,"South");
b2 = new JButton("C");
b2.setFont(new Font("Sans Serif", Font.BOLD, 16));
b2.setForeground(Color.red);
p3.add(b2,"South");
b1.addActionListener(this);
b2.addActionListener(this);
add(p1,"North");
add(p2,"Center");
add(p3,"South");
setVisible(true);
pack();
}
/** styr händelsehanteringen i räknaren */
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == bIndex[20]) //Knappen ","
{
}
if(e.getSource() == bIndex[21]) //Knappen "+"
{
count();
sign = "+";
t1.setText("" + talTemp);
}
if(e.getSource() == bIndex[22]) //Knappen "="
{
count();
tal = talTemp;
sign = " ";
t1.setText("" + talTemp);
}
if(e.getSource() == bIndex[23]) //Knappen "ln"
{
/*count();
sign = "ln";*/
talTemp = tal;
tal = Math.log(talTemp);
t1.setText("" + tal);
}
if(e.getSource() == b1) //Knappen CE
{
tal = 0;
t1.setText("" + tal);
}
if(e.getSource() == b2) //Knappen C
{
tal = 0;
talTemp= 0;
t1.setText(" " + tal);
}
}
/** beräknar resultatet av den matematiska operationen */
public void count()
{
if(talTemp>0){
if(sign == "+")
tal = talTemp + tal;
if(sign == "-")
tal = talTemp - tal;
if(sign == "*")
tal = talTemp * tal;
if(sign == "/")
tal = talTemp / tal;
if(sign == "%")
tal = talTemp * 0.01;
if(sign == "sqrt")
tal = Math.sqrt(talTemp);
if(sign == "1/x")
tal = 1 /talTemp;
if(sign == "=")
tal = talTemp + tal;
if(sign == "sin")
tal = Math.sin(talTemp);
if(sign == "cos")
tal = Math.cos(talTemp);
if(sign == "log")
tal = Math.log(talTemp);
}
talTemp = tal;
tal = 0;
}
public static void main(String args[])
{
CounterApplication c = new CounterApplication();
}
}
答案 0 :(得分:0)
如果您使用分隔符按钮在用户输入字符串中插入空格,则可以调用userInput.split(" ")
,这将为您提供输入所有单个数字的String[]
数组。
String userInput = "100 200 300";
String[] numberStrings = userInput.split(" ");
List<BigDecimal> numbers = Stream.of(numberStrings).map(BigDecimal::new).collect(Collectors.toList());
System.out.println(numbers); // prints [100, 200, 300]
您可以将用户输入作为String
,也许使用java.util.Scanner
,允许小数点。然后在BigDecimal
的构造函数中使用该字符串。试试这个:
String userInput = "1234.5678";
BigDecimal number = new BigDecimal(userInput);
System.out.println(number); // prints '1234.5678'