Java 使用传入参数作为计算器的输入字符串执行计算
如果我运行计算器,我希望它开始自动计算给定参数,例如4+4-2。当没有参数时,他只要求用户插入数字(扫描仪)。这是我的代码。因此需要将参数分配给我的inputString。如果没有参数,扫描仪将要求用户输入一些内容 Main 计算器Java 使用传入参数作为计算器的输入字符串执行计算,java,args,Java,Args,如果我运行计算器,我希望它开始自动计算给定参数,例如4+4-2。当没有参数时,他只要求用户插入数字(扫描仪)。这是我的代码。因此需要将参数分配给我的inputString。如果没有参数,扫描仪将要求用户输入一些内容 Main 计算器 package com.haynespro.calculator; 导入java.util.ArrayList; 公共类计算器{ 公共无效起始计算器(字符串输入字符串){ //将字符串“res”的ArrayList指定给splitExpression ArrayLi
package com.haynespro.calculator;
导入java.util.ArrayList;
公共类计算器{
公共无效起始计算器(字符串输入字符串){
//将字符串“res”的ArrayList指定给splitExpression
ArrayList res=拆分表达式(inputString);
//创建一个包含res的ObjectList
ArrayList objectList=新的ArrayList(res);
System.out.print(“\n让我的算法处理它:\n\n”);
//循环遍历objectList并将字符串转换为双精度字符串
对于(int i=0;i
您没有验证args.length
以确定传入了多少个参数
我将给你一个简化的版本,因为我不知道你应该如何读取/解析args
字符串输入;
如果(参数长度<1){
//您需要一个扫描仪来读取计算器的输入
输入=新扫描仪(System.in).nextLine();
}否则{
//您需要分析args
input=String.join(“,args);
}
新计算器().开始计算器(输入);
使用args更改main(也可以在main类中进行一些重构):
分享Calculator类的代码您几乎就要完成了您只需获取参数并将其传递给Calculator,如果没有参数,则您可以询问它们这是我的问题,因为新手我一直在接近解决方案,但无法获得完整的解决方案:(提示1:
inputString=inputString+arg+”;
即,添加(每个)arg后面跟一个空格,指向inputString
(将其初始化为空)。提示2:要删除逗号的replace命令没有任何作用,因为此时字符串仅为“0”
。您不必加入args,因为示例中没有空格“4+4-2”,仅足以使用args[0]@但一个例子只是一个例子,缺少规范,所以我们可以猜测……可能还需要对每个实例分别进行一次计算argument@Amirates但请删除多余的替换-“0”
没有,
(或在读取用户输入后移动,如有必要)
package com.haynespro.calculator;
import java.util.Scanner;
public class CharAtExample {
public static void main(String[] args) {
for (String arg : args) {
System.out.println(arg);
}
// inputString with scanner
String inputString = "0";
inputString = inputString.replace(",", "");
Scanner userInput = new Scanner(System.in);
System.out.print("please insert your calculations: ");
inputString = userInput.next();
userInput.close();
Calculator calculator = new Calculator();
calculator.startCalculator(inputString);
}// end of main
}// end of class
package com.haynespro.calculator;
import java.util.ArrayList;
public class Calculator {
public void startCalculator(String inputString) {
// Assign ArrayList of Strings "res" to splitExpression
ArrayList<String> res = splitExpression(inputString);
// Create an ObjectList that holds res
ArrayList<Object> objectList = new ArrayList<Object>(res);
System.out.print("\nLet my algorithm take care of it: \n\n");
// Loop through the objectList and convert strings to doubles
for (int i = 0; i < objectList.size(); i++) {
try {
objectList.set(i, Double.parseDouble((String)
objectList.get(i)));
} catch (NumberFormatException nfe) {
}
}
// Create a variable maxi to substract 2 from the objectList index
int maxi = objectList.size();
maxi = maxi - 2;
// Create variable lastSum out of the incoming for-loop's scope.
double lastSum = 0;
// Loop through the objectList with an algorhitm and perform
calculations with
// invoking the sum method
for (int i = 0; i < maxi; i += 2) {
String operator = (String) objectList.get(i + 1);
double a = (Double) objectList.get(i);
double b = (Double) objectList.get(i + 2);
double sum;
if (i == 0) {
sum = sum(a, b, operator);
} else {
sum = sum(lastSum, b, operator);
}
lastSum = sum;
System.out.println(lastSum);
}
}
// Method that matches the string input with operators to perform
calculations.
public static double sum(Double a, Double b, String operator) {
if (operator.equals("+")) {
return a + b;
}
if (operator.equals("-")) {
return a - b;
}
if (operator.equals("*")) {
return a * b;
}
if (operator.equals("/")) {
return a / b;
}
return 0;
}
// ArrayList splitExpression that casts to inputString
private static ArrayList<String> splitExpression(String inputString) {
// ArrayList result to return the result
ArrayList<String> result = new ArrayList<String>();
// Uses the toCharArray method to insert the string reference per
character into
// an array
char[] destArray = inputString.toCharArray();
// Empty String created
String token = "";
// Iterate through the "Items" in the Array
for (int i = 0; i < destArray.length; i++) {
// Nice all those references but we need an Object that actually
holds the array
char c = destArray[i];
// If not a number then add to token, else assign the value of c to
token
if (isBreakCharacter(c)) {
result.add(token);
result.add(Character.toString(c));
token = "";
} else
token = token + c;
}
result.add(token);
return result;
}
// a method that breaks characters which are not numbers.The object "c" also
// needs to hold this method.
public static boolean isBreakCharacter(char c) {
return c == '+' || c == '*' || c == '-' || c == '/';
}
}
String input;
if (args.length < 1) {
// you need a Scanner to read an input for the calculator
input = new Scanner(System.in).nextLine();
} else {
// you've got args to parse
input = String.join("", args);
}
new Calculator().startCalculator(input);
public static void main(String[] args) {
String input=null;
if(args.length>0){
input=args[0];
System.out.println(input);
}else{
input=askForInput();
}
Calculator calculator = new Calculator();
calculator.startCalculator(input);
}// end of main
private static String askForInput() {
// inputString with scanner
Scanner userInput = new Scanner(System.in);
System.out.print("please insert your calculations: ");
String inputString;
try {
inputString = userInput.next();
} finally {
userInput.close();
}
return inputString;
}