需要帮助Split String来创建计算器 [英] Need help with Split String to create a calculator

查看:82
本文介绍了需要帮助Split String来创建计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一篇文章,希望我能提供足够的信息。我正在运行windows xp,使用jGrasp编写代码。我需要制作一个计算器,用户输入2个浮点数和一个操作,并用原始方程输出答案。我想我需要使用split函数,但是一旦它被拆分我不知道该怎么做。这就是我到目前为止 -


import javax.swing。*;

import java.awt。*;

import java.util。*;

public class Calculator {

public static void main(String [] args){

String inputString = JOptionPane .showInputDialog(" blah blah");

String [] result = inputString.split(" // s");

for(int x = 0; x< result.length; x ++)

}

}


我是新手,如果你不能告诉我,对不起,如果这是非常基本的。在我看来,我想给字符串的每个部分一个名字或字母,然后将其用于其余的代码。我不知道这是否可行。输入 - 2 + 1 - 会给出a = 2 b = + c = 1,那么我可以使用if语句来分隔操作(我需要+ - / *和%(余数))。谢谢! Max

解决方案

另外,我发现了对数组的引用,所以也许这就是我的解决方案?如果有人有导游吗?

谢谢


按小步骤完成,并确保当前步骤正确完成,然后再进入下一步。 />
1.)确保从用户正确获取输入。 System.out.print将其输出到控制台进行测试。

2.)确保您的拆分按预期工作。打印出数组中的每个值来测试它。(可以使用java.util.Arrays.toString)。

3.)现在你可以继续进行计算,具体取决于第二个值在你的数组中是。



以小步骤完成并确保当前步骤正确完成,然后再转到下一步。

1.)确保您正确地从用户那里获得输入。 System.out.print将其输出到控制台进行测试。

2.)确保您的拆分按预期工作。打印出数组中的每个值来测试它。(可以使用java.util.Arrays.toString)。

3.)现在你可以继续进行计算,具体取决于第二个值在你的数组中。



我的要求限制了我的一个输出,这会更容易标记,并制作令牌吗?谢谢!

This is my first post, so hopefully I can give enough information. I am running windows xp, using jGrasp to write code. I need to make a calculator in which the user inputs 2 floating point numbers and an operation, with and output of the answer with the original equation. I think I need to use the split function, however I dont know what to do once it is split. Here is what I have so far-

import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Calculator{
public static void main (String[] args){
String inputString = JOptionPane.showInputDialog("blah blah");
String[] result = inputString.split("//s");
for (int x=0; x<result.length; x++)
}
}

I''m new, if you can''t tell, so im sorry if this is extremely elementary. In my mind, I would want to give each part of the string a name or letter, then use those for the rest of the code. I dont know if this is possible. Ex- input- "2 + 1" would give a=2 b=+ c=1, then I could go about using if statements to separate the operations (I need +-/* and % (remainder)). Thanks! Max

解决方案

Also, ive found references to arrays, so maybe thats my solution? If someone has a guide?
Thanks


Do it in small steps and satisfy yourself that the current step is correctly done before moving to the next step.
1.) Make sure you are getting the input correctly from the user. System.out.print it out to the console to test this.
2.) Make sure your splitting is working as expected. Print out each value in the array to test this.(Can use java.util.Arrays.toString for this).
3.) Now you can proceed to do the calculation depending on what the second value in your array is.


Do it in small steps and satisfy yourself that the current step is correctly done before moving to the next step.
1.) Make sure you are getting the input correctly from the user. System.out.print it out to the console to test this.
2.) Make sure your splitting is working as expected. Print out each value in the array to test this.(Can use java.util.Arrays.toString for this).
3.) Now you can proceed to do the calculation depending on what the second value in your array is.

My requirements limit me to the one output, would this be easier to tokenize, and make tokens? Thanks!


这篇关于需要帮助Split String来创建计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆