Java将存储在arraylist中的字符串解析到计算中
因此,我有一个ArrayList,它存储像:{2.0,+,2.0,-,1.0}这样的信息,我需要将其解析为2+2-1,但是我所做的方法不起作用 方法代码:Java将存储在arraylist中的字符串解析到计算中,java,string,math,arraylist,double,Java,String,Math,Arraylist,Double,因此,我有一个ArrayList,它存储像:{2.0,+,2.0,-,1.0}这样的信息,我需要将其解析为2+2-1,但是我所做的方法不起作用 方法代码: public static void ans() { Double cV = Double.parseDouble(calculate.get(0)); for(int i = 1; i < calculate.size(); i += 2) { switch(calculate.get(i)
public static void ans()
{
Double cV = Double.parseDouble(calculate.get(0));
for(int i = 1; i < calculate.size(); i += 2)
{
switch(calculate.get(i))
{
case "+":
cV += Double.parseDouble(calculate.get(i + 1));
break;
case "-":
cV -= Double.parseDouble(calculate.get(i + 1));
break;
}
}
calc.setText("= " + cV);
}
编辑:短代码:
package net.discfiresoftworks.shortcalc;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Short extends JFrame
{
private static final long serialVersionUID = 1L;
public static ArrayList<String> calc = new ArrayList<String>();
public static JLabel ans = new JLabel("");
public static void main(String[] args)
{
new Short();
}
public Short()
{
this.setSize(300, 300);
this.setDefaultCloseOperation(3);
this.setLayout(new FlowLayout());
JButton b1 = new JButton("Click me");
JButton b2 = new JButton("then me");
JButton b3 = new JButton("then me.");
b1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0)
{
addTo("1");
}
});
b2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0)
{
addTo("+");
}
});
b3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0)
{
addTo("1");
ans();
}
});
this.add(b1);
this.add(b2);
this.add(b3);
this.add(ans);
this.setVisible(true);
}
public static void addTo(String toAdd)
{
try{
if(!isNumeric(toAdd))
{
if(!isNumeric(calc.get(calc.size() - 1)))
{
calc.set(calc.size() - 1, toAdd);
}
}else{
calc.add(toAdd);
}
}catch(Exception e){ }
}
public static boolean isNumeric(String str)
{
try{
Double.parseDouble(str);
}catch(NumberFormatException nfe){
return false;
}
return true;
}
public static void ans()
{
Double cV = Double.parseDouble(calc.get(0));
System.out.println(calc.size());
for(int i = 1; i < calc.size(); i += 2)
{
switch(calc.get(i))
{
case "+":
cV += Double.parseDouble(calc.get(i + 1));
break;
}
}
ans.setText("= " + cV);
}
}
这应该实现我所使用的技巧,即计算形式为{2.0,+,2.0,-,1.0}的数组: 但是,如果您的数组如下所示,这将无法正常工作:
{"2.0", "+", "5.0", "7.0"}
因为它会将5和7相加,因为它存储了您使用的最后一个符号,所以您可能需要实现某种取值方法,该方法需要数字之间的符号。但如果您确定输入的总是数字、符号、数字,那么这段代码就不会有问题。您可以使用*和+运算符吗?如果是,{2.0,+,2.0,*,2.0}的结果应该是什么?我仍然要加*,但我有+我正在将cV设置为第一个数字@Victor K。这里到底出了什么问题?你的代码对我来说似乎很好。说了些别的。您确定列表的内容吗?也许是2+2-2?
public static void ans() {
Double total = 0.0;
boolean isSum = true;
for (String input : calculate) {
switch (input) {
case "+":
isSum = true;
break;
case "-":
isSum = false;
break;
default:
if (isSum)
total += Double.parseDouble(input);
else
total -= Double.parseDouble(input);
break;
}
}
/*
* Now you have the total variable which stores the sum.
* You can do whatever you want with it, like printing
* it along with the result.
*/
for (String input : calculate) {
System.out.print(input+" ");
}
System.out.print(" = "+total);
}
{"2.0", "+", "5.0", "7.0"}