Java 整数形式的字符串方程答案
我在一个低级java类中,需要使用java和JavaScript制作一个应用程序。我决定创建一个游戏,使用加法、减法、乘法和除法创建一个随机方程。我遇到的问题是,我的方程式是以字符串的形式创建的,我需要能够计算出答案,并将其与用户的答案进行比较。我以为我有一个下降的解决方案,但我不断得到一些问题。你能帮忙吗?我的代码如下:Java 整数形式的字符串方程答案,java,string,int,Java,String,Int,我在一个低级java类中,需要使用java和JavaScript制作一个应用程序。我决定创建一个游戏,使用加法、减法、乘法和除法创建一个随机方程。我遇到的问题是,我的方程式是以字符串的形式创建的,我需要能够计算出答案,并将其与用户的答案进行比较。我以为我有一个下降的解决方案,但我不断得到一些问题。你能帮忙吗?我的代码如下: import java.util.*; public class GameClient { private static Stack<String> o
import java.util.*;
public class GameClient {
private static Stack<String> operations;
private static Stack<Integer> numbers;
private static String[] tokens;
private static int i;
private static Stack<String> subAdd;
private static Stack<Integer> restOfNumbers;
public static void main(String[] args) {
Random variable = new Random();
int numberOfVariables = variable.nextInt(5)+2; //determines length of equation
String equation = "";
equation = equationGenerator(equation, numberOfVariables, variable);
System.out.println(equation);
System.out.print(calculateAnswer(equation));
}
public static String equationGenerator(String equation, int numberOfVariables, Random variable){
int operation;
int var;
var = variable.nextInt(10)+1;
equation += var;
operation = variable.nextInt(4)+1;
if(numberOfVariables == 1){
equation += " = ";
return equation;
}
if(operation == 1)
{
equation += " + ";
}
else if(operation == 2)
{
equation += " - ";
}
else if(operation == 3)
{
equation += " / ";
}
else if(operation == 4)
{
equation += " * ";
}
return equationGenerator(equation, numberOfVariables-1, variable);
}
public static int calculateAnswer(String equation){
String delims = "[ ]+";
tokens = equation.split(delims);
for(i=0; i< tokens.length; i++) //does all multiplication and division first leaving just addition and subtraction
switch(tokens[i]){
case "0": case "1":case "2":case "3":case "4":case "5":case "6":case "7":case "8":case "9":
int number = Integer.parseInt(tokens[i]);
popOrPush(number);
break;
case "*": case "/": case "+": case "-":
popOrPush(tokens[i], tokens);
}
while(!numbers.empty()){ //flips number and operation stacks to do addition and subtraction in correct order
restOfNumbers.push(numbers.pop());
}
while(!operations.empty()){
subAdd.push(operations.pop());
}
while(!subAdd.empty()){
switch(subAdd.pop()){
case "+":
restOfNumbers.push(restOfNumbers.pop() + restOfNumbers.pop());
case "-":
restOfNumbers.push(restOfNumbers.pop() - restOfNumbers.pop());
}
}
return restOfNumbers.pop();
}
public static void popOrPush(int number){
numbers.push(number);
}
public static void popOrPush(String operation, String[] tokens){
switch(operation){
case "*":
int multipliedValue = numbers.pop();
i++;
multipliedValue = multipliedValue * Integer.parseInt(tokens[i]);
numbers.push(multipliedValue);
case "/":
int dividedValue = numbers.pop();
i++;
dividedValue = dividedValue / Integer.parseInt(tokens[i]);
numbers.push(dividedValue);
case "+": case "-":
operations.push(operation);
}
}
}
您没有初始化堆栈,因此在尝试使用numbers变量时可能会得到NPE 确保初始化所有变量,尤其是堆栈对象。例如:
Stack<Integer> numbers = new Stack<>();
那么,它是java还是javascript?因为我有消息要告诉你,不可能两者都有。当你不知道标签的含义时,为什么还要添加标签?如果是Java,请看下面的答案。这是Java而不是Javascript。好的,酷,是它的Java。但这不是问题所在。对不起,我不知道我到底在说什么,所以我在这个网站上。有人知道为什么我在尝试运行此程序时会出现NullPointerException吗?它必须在calculateAnswer方法或popOrPush方法中,因为它将创建没有问题的方程。我一直得到一些问题是一个完全无用的问题描述。如果您有错误,发布堆栈跟踪,描述您为解决该错误所做的工作,并解释具体的问题所在。