标签:gif 改变 浮点 params close 格式 current open sub
* + 11.0 12.0 + 24.0 35.0
1357.000000
import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[]args) { Scanner sc=new Scanner(System.in); String []news=sc.nextLine().split(" "); Stack<Double>stack=new Stack<Double>(); for(int i=news.length-1;i>=0;i--) { if(news[i].equals("+")) { stack.push((stack.pop()+stack.pop())); } else if(news[i].equals("-")) { stack.push((stack.pop()-stack.pop())); }else if(news[i].equals("*")) { stack.push((stack.pop()*stack.pop())); }else if(news[i].equals("/")) { stack.push((stack.pop()/stack.pop())); }else { try { stack.push(Double.parseDouble(news[i])); } catch (NumberFormatException e) { // TODO 自动生成的 catch 块 continue; } } } System.out.printf("%.6f\n",stack.peek()); } }
感谢董卫老师的帮助,找出我的bug。注意输出格式。老师代码:
import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); String[] ss=sc.nextLine().split(" "); Stack<Double> st=new Stack<Double>(); for(int i=ss.length-1;i>=0;i--){ try{ double d=Double.parseDouble(ss[i]); st.push(d); }catch(Exception e){ if("+".equals(ss[i])){ st.push(st.pop()+st.pop()); }else if("-".equals(ss[i])){ st.push(st.pop()-st.pop()); }else if("*".equals(ss[i])){ st.push(st.pop()*st.pop()); }else{ st.push(st.pop()/st.pop()); } } } System.out.printf("%.6f\n", st.peek()); } }
标签:gif 改变 浮点 params close 格式 current open sub
原文地址:https://www.cnblogs.com/dean-SunPeishuai/p/10583168.html