创建”计算器”以评估 Java 中的算术表达式 – 代码问题

Creating a "calculator" to evaluate arithmetic expressions in Java - code troubles

我试图通过创建一个简单的计算器来处理涉及括号的算术表达式来巩固我对堆栈和运算符的理解。我觉得我的代码应该可以工作,但它肯定不会给我正确的输出。

尽管我有一个方法来评估每个表达式,但当我尝试返回数字堆栈时,它不会打印出任何评估的方法,而只会打印出用户输入的所有数字。我也想处理输入中的问题,例如不匹配的运算符或缺少括号。

我尝试使用 9 * 5 或 (7 * 6) (9 - 4) 之类的简单表达式运行代码,但无论如何它都会返回最后一个双精度数。

到目前为止,这是我的代码:

主要方法

import java.util.Stack;

import javax.swing.JOptionPane;



public class Calculator {



  // instance variables

  private Stack < Double > nums;

  private Stack < String > ops; 

  String list;



    // constructor

  public Calculator()

  {

    nums = new Stack < Double > ();

    ops = new Stack < String > ();

  }



  // methods



  public static boolean isDouble(String str) {

    try {

      Double.parseDouble(str);

    } catch (NumberFormatException e) {

      return false;

    } catch (NullPointerException e) {

      return false;

    }

    return true;

  }



  public static boolean isValidOp(String str) {

    return (str =="(" || str ==")" || str =="^" || str =="*" || str =="/" || str =="+" || str =="-");

  }



  public int prec(String str) {

    if (str =="(" || str ==")")

      return 4;

    if (str =="^")

      return 3;

    if (str =="*" || str =="/")

      return 2;

    if (str =="+" || str =="-")

      return 1;

    else

      return -1;

  }



  public double applyOperator(double left, String op, double right) {

    if (op =="+") {

      return (left + right);

    }

    if (op =="-") {

      return (left - right);

    }

    if (op =="*") {

      return (left * right);

    }

    if (op =="/") {

      return (left / right);

    }

    if (op =="^") {

      return Math.pow(left, right);

    } else {

      throw new IllegalArgumentException("Not a valid operator");

    }

  }



  public String evaluate(String str)

  {  

    String [] tokens = str.split("");



    for (int i = 0; i < tokens.length; i++)

    {

      if (isDouble(tokens [i]) == true)

      {

        nums.push(Double.parseDouble(tokens [i]));

      }  

      if (tokens [i] =="(")

      {

        ops.push(tokens [i]);

      }

      if (tokens [i] ==")")

      {

        String op1 = ops.pop();

        double num1 = nums.pop();

        double num2 = nums.pop();

        double result = applyOperator(num1,op1,num2);

        nums.add(result);

      }

      if (tokens [i] =="+" || tokens [i] =="-" || tokens [i] =="*" || tokens [i] =="/" || tokens [i] =="^")

      {

        if(ops.isEmpty())

        {

          ops.push(tokens [i]);

        }

        else if (prec(tokens [i]) > prec(ops.peek()))

        {

          ops.push(tokens [i]);

        }

        else if (prec(tokens [i]) < prec(ops.peek()) && !ops.isEmpty() && ops.peek() !="(")

        {

          String ac1 = ops.pop();

          double res1 = nums.pop();

          double res2 = nums.pop();

          double outcome = applyOperator(res1,ac1,res2);

          nums.add(outcome);

        }  

      }

    }



    while(!ops.isEmpty() && nums.size() > 1)

    {

      String ab = ops.pop();

      double bb = nums.pop();

      double cb = nums.pop();

      double clac = applyOperator(bb,ab,cb);

      nums.add(clac);

    }

    String fix = nums.pop().toString();

    return fix;

  }

}

import javax.swing.JOptionPane;



public class AppforCalc {



  public static void main(String [] args)

  {

    Calculator calc = new Calculator();

    String reply ="yes";

    String instructions ="Enter a mathematical expression. Separate everything with spaces";



    while(reply.equalsIgnoreCase("yes"))

    {

      String expression = JOptionPane.showInputDialog(instructions);

      String ans = calc.evaluate(expression);

      reply = JOptionPane.showInputDialog("The solution is" + ans +"Try again?");

    }

  }

}

while(!ops.isEmpty() && nums.size() > 1)

    {

      String ab = ops.pop();

      double bb = nums.pop();

      double cb = nums.pop();

      double clac = applyOperator(bb,ab,cb);

      nums.add(clac);

    }
    else if (prec(tokens [i]) < prec(ops.peek()) && !ops.isEmpty() && ops.peek() !="(")

    {

      String ac1 = ops.pop();

      double res1 = nums.pop();

      double res2 = nums.pop();

      double outcome = applyOperator(res1,ac1,res2);

      nums.add(outcome);

    }

else if (prec(tokens [i]) < prec(ops.peek()) && !ops.isEmpty() && ops.peek() !="(")

    {

      ...

      ...

      nums.add(outcome); // I highly suggest refactoring this to nums.push due to readability considerations

      ops.push(tokens[i]);

    }

测试者:

import java.util.Stack;

import javax.swing.JOptionPane;



public class Calculator {



  // instance variables

  private Stack < Double > nums;

  private Stack < String > ops; 

  String list;



    // constructor

  public Calculator()

  {

    nums = new Stack < Double > ();

    ops = new Stack < String > ();

  }



  // methods



  public static boolean isDouble(String str) {

    try {

      Double.parseDouble(str);

    } catch (NumberFormatException e) {

      return false;

    } catch (NullPointerException e) {

      return false;

    }

    return true;

  }



  public static boolean isValidOp(String str) {

    return (str =="(" || str ==")" || str =="^" || str =="*" || str =="/" || str =="+" || str =="-");

  }



  public int prec(String str) {

    if (str =="(" || str ==")")

      return 4;

    if (str =="^")

      return 3;

    if (str =="*" || str =="/")

      return 2;

    if (str =="+" || str =="-")

      return 1;

    else

      return -1;

  }



  public double applyOperator(double left, String op, double right) {

    if (op =="+") {

      return (left + right);

    }

    if (op =="-") {

      return (left - right);

    }

    if (op =="*") {

      return (left * right);

    }

    if (op =="/") {

      return (left / right);

    }

    if (op =="^") {

      return Math.pow(left, right);

    } else {

      throw new IllegalArgumentException("Not a valid operator");

    }

  }



  public String evaluate(String str)

  {  

    String [] tokens = str.split("");



    for (int i = 0; i < tokens.length; i++)

    {

      if (isDouble(tokens [i]) == true)

      {

        nums.push(Double.parseDouble(tokens [i]));

      }  

      if (tokens [i] =="(")

      {

        ops.push(tokens [i]);

      }

      if (tokens [i] ==")")

      {

        String op1 = ops.pop();

        double num1 = nums.pop();

        double num2 = nums.pop();

        double result = applyOperator(num1,op1,num2);

        nums.add(result);

      }

      if (tokens [i] =="+" || tokens [i] =="-" || tokens [i] =="*" || tokens [i] =="/" || tokens [i] =="^")

      {

        if(ops.isEmpty())

        {

          ops.push(tokens [i]);

        }

        else if (prec(tokens [i]) > prec(ops.peek()))

        {

          ops.push(tokens [i]);

        }

        else if (prec(tokens [i]) < prec(ops.peek()) && !ops.isEmpty() && ops.peek() !="(")

        {

          String ac1 = ops.pop();

          double res1 = nums.pop();

          double res2 = nums.pop();

          double outcome = applyOperator(res1,ac1,res2);

          nums.add(outcome);

        }  

      }

    }



    while(!ops.isEmpty() && nums.size() > 1)

    {

      String ab = ops.pop();

      double bb = nums.pop();

      double cb = nums.pop();

      double clac = applyOperator(bb,ab,cb);

      nums.add(clac);

    }

    String fix = nums.pop().toString();

    return fix;

  }

}

import javax.swing.JOptionPane;



public class AppforCalc {



  public static void main(String [] args)

  {

    Calculator calc = new Calculator();

    String reply ="yes";

    String instructions ="Enter a mathematical expression. Separate everything with spaces";



    while(reply.equalsIgnoreCase("yes"))

    {

      String expression = JOptionPane.showInputDialog(instructions);

      String ans = calc.evaluate(expression);

      reply = JOptionPane.showInputDialog("The solution is" + ans +"Try again?");

    }

  }

}

while(!ops.isEmpty() && nums.size() > 1)

    {

      String ab = ops.pop();

      double bb = nums.pop();

      double cb = nums.pop();

      double clac = applyOperator(bb,ab,cb);

      nums.add(clac);

    }
    else if (prec(tokens [i]) < prec(ops.peek()) && !ops.isEmpty() && ops.peek() !="(")

    {

      String ac1 = ops.pop();

      double res1 = nums.pop();

      double res2 = nums.pop();

      double outcome = applyOperator(res1,ac1,res2);

      nums.add(outcome);

    }

else if (prec(tokens [i]) < prec(ops.peek()) && !ops.isEmpty() && ops.peek() !="(")

    {

      ...

      ...

      nums.add(outcome); // I highly suggest refactoring this to nums.push due to readability considerations

      ops.push(tokens[i]);

    }

算法失败的主要原因是在尝试检查 String 是否相等时使用了 ==

在Java 中,== 是一个布尔运算符,它对所有操作数都具有相同的行为,并检查操作数值的相等性。这意味着原语会按预期进行检查,但作为对象的字符串将导致比较两个字符串的内存引用,只有当这两个字符串实际上是相同的字符串时才会导致结果为真。这意味着必须使用 equals 方法进行字符串相等检查。

计算器的行为存在更多问题(算法问题),但在处理字符串相等检查后,这些问题将更容易识别和修复。必须解决的问题的一个示例是:

import java.util.Stack;

import javax.swing.JOptionPane;



public class Calculator {



  // instance variables

  private Stack < Double > nums;

  private Stack < String > ops; 

  String list;



    // constructor

  public Calculator()

  {

    nums = new Stack < Double > ();

    ops = new Stack < String > ();

  }



  // methods



  public static boolean isDouble(String str) {

    try {

      Double.parseDouble(str);

    } catch (NumberFormatException e) {

      return false;

    } catch (NullPointerException e) {

      return false;

    }

    return true;

  }



  public static boolean isValidOp(String str) {

    return (str =="(" || str ==")" || str =="^" || str =="*" || str =="/" || str =="+" || str =="-");

  }



  public int prec(String str) {

    if (str =="(" || str ==")")

      return 4;

    if (str =="^")

      return 3;

    if (str =="*" || str =="/")

      return 2;

    if (str =="+" || str =="-")

      return 1;

    else

      return -1;

  }



  public double applyOperator(double left, String op, double right) {

    if (op =="+") {

      return (left + right);

    }

    if (op =="-") {

      return (left - right);

    }

    if (op =="*") {

      return (left * right);

    }

    if (op =="/") {

      return (left / right);

    }

    if (op =="^") {

      return Math.pow(left, right);

    } else {

      throw new IllegalArgumentException("Not a valid operator");

    }

  }



  public String evaluate(String str)

  {  

    String [] tokens = str.split("");



    for (int i = 0; i < tokens.length; i++)

    {

      if (isDouble(tokens [i]) == true)

      {

        nums.push(Double.parseDouble(tokens [i]));

      }  

      if (tokens [i] =="(")

      {

        ops.push(tokens [i]);

      }

      if (tokens [i] ==")")

      {

        String op1 = ops.pop();

        double num1 = nums.pop();

        double num2 = nums.pop();

        double result = applyOperator(num1,op1,num2);

        nums.add(result);

      }

      if (tokens [i] =="+" || tokens [i] =="-" || tokens [i] =="*" || tokens [i] =="/" || tokens [i] =="^")

      {

        if(ops.isEmpty())

        {

          ops.push(tokens [i]);

        }

        else if (prec(tokens [i]) > prec(ops.peek()))

        {

          ops.push(tokens [i]);

        }

        else if (prec(tokens [i]) < prec(ops.peek()) && !ops.isEmpty() && ops.peek() !="(")

        {

          String ac1 = ops.pop();

          double res1 = nums.pop();

          double res2 = nums.pop();

          double outcome = applyOperator(res1,ac1,res2);

          nums.add(outcome);

        }  

      }

    }



    while(!ops.isEmpty() && nums.size() > 1)

    {

      String ab = ops.pop();

      double bb = nums.pop();

      double cb = nums.pop();

      double clac = applyOperator(bb,ab,cb);

      nums.add(clac);

    }

    String fix = nums.pop().toString();

    return fix;

  }

}

import javax.swing.JOptionPane;



public class AppforCalc {



  public static void main(String [] args)

  {

    Calculator calc = new Calculator();

    String reply ="yes";

    String instructions ="Enter a mathematical expression. Separate everything with spaces";



    while(reply.equalsIgnoreCase("yes"))

    {

      String expression = JOptionPane.showInputDialog(instructions);

      String ans = calc.evaluate(expression);

      reply = JOptionPane.showInputDialog("The solution is" + ans +"Try again?");

    }

  }

}

while(!ops.isEmpty() && nums.size() > 1)

    {

      String ab = ops.pop();

      double bb = nums.pop();

      double cb = nums.pop();

      double clac = applyOperator(bb,ab,cb);

      nums.add(clac);

    }
    else if (prec(tokens [i]) < prec(ops.peek()) && !ops.isEmpty() && ops.peek() !="(")

    {

      String ac1 = ops.pop();

      double res1 = nums.pop();

      double res2 = nums.pop();

      double outcome = applyOperator(res1,ac1,res2);

      nums.add(outcome);

    }

else if (prec(tokens [i]) < prec(ops.peek()) && !ops.isEmpty() && ops.peek() !="(")

    {

      ...

      ...

      nums.add(outcome); // I highly suggest refactoring this to nums.push due to readability considerations

      ops.push(tokens[i]);

    }

操作数(bbcb)从堆栈中弹出,因此它们以相反的顺序到达(解析时,cbbb 之前被推入堆栈)。这意味着 cb 是左侧操作数, bb 是右侧操作数 -> double clac = applyOperator(cb,ab,bb); 应该对 applyOperand 方法的所有用法进行相同的重构。

另一个问题如下:

import java.util.Stack;

import javax.swing.JOptionPane;



public class Calculator {



  // instance variables

  private Stack < Double > nums;

  private Stack < String > ops; 

  String list;



    // constructor

  public Calculator()

  {

    nums = new Stack < Double > ();

    ops = new Stack < String > ();

  }



  // methods



  public static boolean isDouble(String str) {

    try {

      Double.parseDouble(str);

    } catch (NumberFormatException e) {

      return false;

    } catch (NullPointerException e) {

      return false;

    }

    return true;

  }



  public static boolean isValidOp(String str) {

    return (str =="(" || str ==")" || str =="^" || str =="*" || str =="/" || str =="+" || str =="-");

  }



  public int prec(String str) {

    if (str =="(" || str ==")")

      return 4;

    if (str =="^")

      return 3;

    if (str =="*" || str =="/")

      return 2;

    if (str =="+" || str =="-")

      return 1;

    else

      return -1;

  }



  public double applyOperator(double left, String op, double right) {

    if (op =="+") {

      return (left + right);

    }

    if (op =="-") {

      return (left - right);

    }

    if (op =="*") {

      return (left * right);

    }

    if (op =="/") {

      return (left / right);

    }

    if (op =="^") {

      return Math.pow(left, right);

    } else {

      throw new IllegalArgumentException("Not a valid operator");

    }

  }



  public String evaluate(String str)

  {  

    String [] tokens = str.split("");



    for (int i = 0; i < tokens.length; i++)

    {

      if (isDouble(tokens [i]) == true)

      {

        nums.push(Double.parseDouble(tokens [i]));

      }  

      if (tokens [i] =="(")

      {

        ops.push(tokens [i]);

      }

      if (tokens [i] ==")")

      {

        String op1 = ops.pop();

        double num1 = nums.pop();

        double num2 = nums.pop();

        double result = applyOperator(num1,op1,num2);

        nums.add(result);

      }

      if (tokens [i] =="+" || tokens [i] =="-" || tokens [i] =="*" || tokens [i] =="/" || tokens [i] =="^")

      {

        if(ops.isEmpty())

        {

          ops.push(tokens [i]);

        }

        else if (prec(tokens [i]) > prec(ops.peek()))

        {

          ops.push(tokens [i]);

        }

        else if (prec(tokens [i]) < prec(ops.peek()) && !ops.isEmpty() && ops.peek() !="(")

        {

          String ac1 = ops.pop();

          double res1 = nums.pop();

          double res2 = nums.pop();

          double outcome = applyOperator(res1,ac1,res2);

          nums.add(outcome);

        }  

      }

    }



    while(!ops.isEmpty() && nums.size() > 1)

    {

      String ab = ops.pop();

      double bb = nums.pop();

      double cb = nums.pop();

      double clac = applyOperator(bb,ab,cb);

      nums.add(clac);

    }

    String fix = nums.pop().toString();

    return fix;

  }

}

import javax.swing.JOptionPane;



public class AppforCalc {



  public static void main(String [] args)

  {

    Calculator calc = new Calculator();

    String reply ="yes";

    String instructions ="Enter a mathematical expression. Separate everything with spaces";



    while(reply.equalsIgnoreCase("yes"))

    {

      String expression = JOptionPane.showInputDialog(instructions);

      String ans = calc.evaluate(expression);

      reply = JOptionPane.showInputDialog("The solution is" + ans +"Try again?");

    }

  }

}

while(!ops.isEmpty() && nums.size() > 1)

    {

      String ab = ops.pop();

      double bb = nums.pop();

      double cb = nums.pop();

      double clac = applyOperator(bb,ab,cb);

      nums.add(clac);

    }
    else if (prec(tokens [i]) < prec(ops.peek()) && !ops.isEmpty() && ops.peek() !="(")

    {

      String ac1 = ops.pop();

      double res1 = nums.pop();

      double res2 = nums.pop();

      double outcome = applyOperator(res1,ac1,res2);

      nums.add(outcome);

    }

else if (prec(tokens [i]) < prec(ops.peek()) && !ops.isEmpty() && ops.peek() !="(")

    {

      ...

      ...

      nums.add(outcome); // I highly suggest refactoring this to nums.push due to readability considerations

      ops.push(tokens[i]);

    }

进行了内部评估,但评估的触发是发现具有较低存在的操作数。运算后应将操作数推入操作堆栈:

import java.util.Stack;

import javax.swing.JOptionPane;



public class Calculator {



  // instance variables

  private Stack < Double > nums;

  private Stack < String > ops; 

  String list;



    // constructor

  public Calculator()

  {

    nums = new Stack < Double > ();

    ops = new Stack < String > ();

  }



  // methods



  public static boolean isDouble(String str) {

    try {

      Double.parseDouble(str);

    } catch (NumberFormatException e) {

      return false;

    } catch (NullPointerException e) {

      return false;

    }

    return true;

  }



  public static boolean isValidOp(String str) {

    return (str =="(" || str ==")" || str =="^" || str =="*" || str =="/" || str =="+" || str =="-");

  }



  public int prec(String str) {

    if (str =="(" || str ==")")

      return 4;

    if (str =="^")

      return 3;

    if (str =="*" || str =="/")

      return 2;

    if (str =="+" || str =="-")

      return 1;

    else

      return -1;

  }



  public double applyOperator(double left, String op, double right) {

    if (op =="+") {

      return (left + right);

    }

    if (op =="-") {

      return (left - right);

    }

    if (op =="*") {

      return (left * right);

    }

    if (op =="/") {

      return (left / right);

    }

    if (op =="^") {

      return Math.pow(left, right);

    } else {

      throw new IllegalArgumentException("Not a valid operator");

    }

  }



  public String evaluate(String str)

  {  

    String [] tokens = str.split("");



    for (int i = 0; i < tokens.length; i++)

    {

      if (isDouble(tokens [i]) == true)

      {

        nums.push(Double.parseDouble(tokens [i]));

      }  

      if (tokens [i] =="(")

      {

        ops.push(tokens [i]);

      }

      if (tokens [i] ==")")

      {

        String op1 = ops.pop();

        double num1 = nums.pop();

        double num2 = nums.pop();

        double result = applyOperator(num1,op1,num2);

        nums.add(result);

      }

      if (tokens [i] =="+" || tokens [i] =="-" || tokens [i] =="*" || tokens [i] =="/" || tokens [i] =="^")

      {

        if(ops.isEmpty())

        {

          ops.push(tokens [i]);

        }

        else if (prec(tokens [i]) > prec(ops.peek()))

        {

          ops.push(tokens [i]);

        }

        else if (prec(tokens [i]) < prec(ops.peek()) && !ops.isEmpty() && ops.peek() !="(")

        {

          String ac1 = ops.pop();

          double res1 = nums.pop();

          double res2 = nums.pop();

          double outcome = applyOperator(res1,ac1,res2);

          nums.add(outcome);

        }  

      }

    }



    while(!ops.isEmpty() && nums.size() > 1)

    {

      String ab = ops.pop();

      double bb = nums.pop();

      double cb = nums.pop();

      double clac = applyOperator(bb,ab,cb);

      nums.add(clac);

    }

    String fix = nums.pop().toString();

    return fix;

  }

}

import javax.swing.JOptionPane;



public class AppforCalc {



  public static void main(String [] args)

  {

    Calculator calc = new Calculator();

    String reply ="yes";

    String instructions ="Enter a mathematical expression. Separate everything with spaces";



    while(reply.equalsIgnoreCase("yes"))

    {

      String expression = JOptionPane.showInputDialog(instructions);

      String ans = calc.evaluate(expression);

      reply = JOptionPane.showInputDialog("The solution is" + ans +"Try again?");

    }

  }

}

while(!ops.isEmpty() && nums.size() > 1)

    {

      String ab = ops.pop();

      double bb = nums.pop();

      double cb = nums.pop();

      double clac = applyOperator(bb,ab,cb);

      nums.add(clac);

    }
    else if (prec(tokens [i]) < prec(ops.peek()) && !ops.isEmpty() && ops.peek() !="(")

    {

      String ac1 = ops.pop();

      double res1 = nums.pop();

      double res2 = nums.pop();

      double outcome = applyOperator(res1,ac1,res2);

      nums.add(outcome);

    }

else if (prec(tokens [i]) < prec(ops.peek()) && !ops.isEmpty() && ops.peek() !="(")

    {

      ...

      ...

      nums.add(outcome); // I highly suggest refactoring this to nums.push due to readability considerations

      ops.push(tokens[i]);

    }

参考文献:

  • Java中==和equals的区别
  • 实现科学计算器的指南(在 c 中,用作算法参考)
  • 调车场算法 - 正如 user207421 所建议的那样

相关推荐

  • Spring部署设置openshift

    Springdeploymentsettingsopenshift我有一个问题让我抓狂了三天。我根据OpenShift帐户上的教程部署了spring-eap6-quickstart代码。我已配置调试选项,并且已将Eclipse工作区与OpehShift服务器同步-服务器上的一切工作正常,但在Eclipse中出现无法消除的错误。我有这个错误:cvc-complex-type.2.4.a:Invali…
    2025-04-161
  • 检查Java中正则表达式中模式的第n次出现

    CheckfornthoccurrenceofpatterninregularexpressioninJava本问题已经有最佳答案,请猛点这里访问。我想使用Java正则表达式检查输入字符串中特定模式的第n次出现。你能建议怎么做吗?这应该可以工作:MatchResultfindNthOccurance(intn,Patternp,CharSequencesrc){Matcherm=p.matcher…
    2025-04-161
  • 如何让 JTable 停留在已编辑的单元格上

    HowtohaveJTablestayingontheeditedcell如果有人编辑JTable的单元格内容并按Enter,则内容会被修改并且表格选择会移动到下一行。是否可以禁止JTable在单元格编辑后转到下一行?原因是我的程序使用ListSelectionListener在单元格选择上同步了其他一些小部件,并且我不想在编辑当前单元格后选择下一行。Enter的默认绑定是名为selectNext…
    2025-04-161
  • Weblogic 12c 部署

    Weblogic12cdeploy我正在尝试将我的应用程序从Tomcat迁移到Weblogic12.2.1.3.0。我能够毫无错误地部署应用程序,但我遇到了与持久性提供程序相关的运行时错误。这是堆栈跟踪:javax.validation.ValidationException:CalltoTraversableResolver.isReachable()threwanexceptionatorg.…
    2025-04-161
  • Resteasy Content-Type 默认值

    ResteasyContent-Typedefaults我正在使用Resteasy编写一个可以返回JSON和XML的应用程序,但可以选择默认为XML。这是我的方法:@GET@Path("/content")@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})publicStringcontentListRequestXm…
    2025-04-161
  • 代码不会停止运行,在 Java 中

    thecodedoesn'tstoprunning,inJava我正在用Java解决项目Euler中的问题10,即"Thesumoftheprimesbelow10is2+3+5+7=17.Findthesumofalltheprimesbelowtwomillion."我的代码是packageprojecteuler_1;importjava.math.BigInteger;importjava…
    2025-04-161
  • Out of memory java heap space

    Outofmemoryjavaheapspace我正在尝试将大量文件从服务器发送到多个客户端。当我尝试发送大小为700mb的文件时,它显示了"OutOfMemoryjavaheapspace"错误。我正在使用Netbeans7.1.2版本。我还在属性中尝试了VMoption。但仍然发生同样的错误。我认为阅读整个文件存在一些问题。下面的代码最多可用于300mb。请给我一些建议。提前致谢publicc…
    2025-04-161
  • Log4j 记录到共享日志文件

    Log4jLoggingtoaSharedLogFile有没有办法将log4j日志记录事件写入也被其他应用程序写入的日志文件。其他应用程序可以是非Java应用程序。有什么缺点?锁定问题?格式化?Log4j有一个SocketAppender,它将向服务发送事件,您可以自己实现或使用与Log4j捆绑的简单实现。它还支持syslogd和Windows事件日志,这对于尝试将日志输出与来自非Java应用程序…
    2025-04-161