小编sma*_*ish的帖子

将 JavaScript 输入数字格式化为带有两位小数的浮点数,并对所有操作强制执行

这是我正在构建的应用程序的原型视图:

原型视图

问题是,100就像你所说的那样int- 但我想说100.00,因为它代表金钱$- 所以它应该始终以小​​数点后两位结尾。这意味着 - 本质上我试图创建的表示是一个带有两个尾随小数位的浮点数。

所以,这个应用程序本身是在 React 中构建的,并且状态是按如下方式创建和持久化的,这本质上是我的程序中最重要的文件,它被称为App.js

import React, { Component } from 'react';
import ButtonNumberContainer from './ButtonNumberContainer.js'
import ButtonEquationContainer from './ButtonEquationContainer.js'
import Result from './Result'
import './App.css';

class App extends Component {
  constructor() {
    super()
    this.state = {
      equation: "",
    }

    this.addLogicToEquation = this.addLogicToEquation.bind(this)
    this.evalEquation = this.evalEquation.bind(this)
  }

  addLogicToEquation(newLogic) {
    let equation = this.state.equation

    if(newLogic==="10%"){
      let newEquation = Number(equation) + (Number(equation) * 0.10)
      console.log(newEquation)
      this.setState({equation: newEquation})
    }else …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

2
推荐指数
1
解决办法
1万
查看次数

创建 Java 程序的命令行界面,从终端运行

目前我正在处理一个现有的 Java 项目,我的目标是创建一个命令行界面来向它传递参数,可以这么说来“运行”它。

我正在使用Apache commons cli 库,如下所示:

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class CommandLineParameters {

  public static void main(String[] args) {
    CommandLineParser parser = new DefaultParser();
    Options options = new Options();

    options.addOption("a", "abc", true, "First Parameter");
    options.addOption("x", "xyz", true, "Second Parameter");
    options.addOption("h", "help", false, "Shows this Help");

    try {
      CommandLine commandLine = parser.parse(options, args);

      System.out.println(commandLine.getOptionValue("a"));
      System.out.println(commandLine.getOptionValue("x"));

      if (commandLine.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("CommandLineParameters", options);
      }

    } catch (ParseException …
Run Code Online (Sandbox Code Playgroud)

java terminal command-line-interface

1
推荐指数
1
解决办法
1万
查看次数

计算哈希映射的指标的平均值

我尝试编写这段代码来测试我如何计算哈希映射的类似索引的平均值的想法.
即,如果第一个数组2的第一个值是,第二个数组 4的第一个值是第一个值,第三个数组的第一个值是3,我希望为(4 + 3 +)赋值,对于hashmap中包含的每个数组2/3)= 3到第一个索引的最后double []数组,依此类推所有索引2到n.

    int Size = 3;

    double[] AVERAGED_WEIGHTS = new double[Size];

    //store weights to be averaged. 
    Map<Integer,double[]> cached_weights = new HashMap<Integer,double[]>();

    double[] weights = new double[Size];

   int iteration = 0;
   do 
   {
       weights[iteration] = Math.floor(Math.random() * 10000) / 10000;

       iteration++;

       //store weights for averaging
       cached_weights.put( iteration , weights );  
    } 
    while (iteration < Size);

    //calc averages
    for (Entry<Integer, double[]> entry : cached_weights.entrySet()) 
    {
        int key = entry.getKey();
        double[] …
Run Code Online (Sandbox Code Playgroud)

java hashmap

0
推荐指数
1
解决办法
741
查看次数

滚动我自己的对象来替换Map <int [],String>&Map <ArrayList <String>,String>

我正在尝试使用以下数据结构:

//all the words in each file as index, the label as value
static Map< ArrayList<String> , String > train__list_of_file_words = new HashMap<>();
static Map< ArrayList<String> , String > test__list_of_file_words = new HashMap<>();

//frequency count against global dictionary
static Map< int[] , String > train_freq_count_against_globo_dict = new HashMap<>();
static Map< int[] , String > test_freq_count_against_globo_dict = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)

但我被告知这是非感性的,因为它们不能与equals()一起顺利工作并且是可变的.

我想解决方案是编写我自己的对象以类似的方式存储这些信息,但我以前从未这样做过.怎么做?

java object

0
推荐指数
1
解决办法
91
查看次数