从用户编号输入中获取总和

发布于 2025-01-30 18:07:05 字数 2483 浏览 3 评论 0原文

我正在尝试从所有用户数量输入中获得一笔汇率。 用户从对话框中输入数量号。该数量是在阵列列表中的文本视图中显示的。 在阵列数量中,我可以获得位置(0)和位置(1)。 但是,这个想法是将所有阵列列表的位置盖出来,并确定数字的总和。

我正在尝试使用此代码,但显然有些不对劲。

int total1 = Integer.parseInt(quantity.get(0));
            int total2 = Integer.parseInt(quantity.get(1));
            int total = 0;
            for (int i = 0; i < quantity.size(); i++) {
                total = total2 + total1 ;
            }
            System.out.println(Integer.valueOf(total));

这是活动

public class CalculatorActivity extends AppCompatActivity {

Button btnCclcola;
TextView cancel, doneBtn, nameTV, quantityTV, resulttry;
EditText ingrName, ingrQuantity;
Dialog dialog;
RecyclerView recyclerView;
ArrayList<String> text = new ArrayList<>();
ArrayList<String> quantity = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calculator);

    nameTV = findViewById(R.id.nameTV);
    quantityTV = findViewById(R.id.qunatityTV);
    resulttry = findViewById(R.id.resulttry);
    btnCclcola = findViewById(R.id.btn_calcola);

    recyclerView = findViewById(R.id.rv_calculator);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    CalculatorAdapter calculatorAdapter = new CalculatorAdapter(this, text, quantity);
    recyclerView.setAdapter(calculatorAdapter);

    btnCclcola.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            int total1 = Integer.parseInt(quantity.get(0));
            int total2 = Integer.parseInt(quantity.get(1));
            int total = 0;
            for (int i = 0; i < quantity.size(); i++) {
                total = total2 + total1 ;
            }
            System.out.println(Integer.valueOf(total));
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {...}
   

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {...}
    

private void opendialog() {...}


private void validateCategory() {...}
   

private void saveInfo() {
    String ingName = ingrName.getText().toString();
    int ingQuant = Integer.valueOf(ingrQuantity.getText().toString());
    
    text.add(ingName);
    quantity.add(String.valueOf(Integer.valueOf(ingQuant)));

}

I am trying to get a sum from all the user numbers input.
User enter a quantity number from dialog. The quantity is the shown on a TextView, inside the ArrayList quantity.
In the ArrayList quantity, i can get position(0) and position(1).
But the idea is to ge all the Arraylist positions and esecute the sum of the numbers.

I am trying with this code, but obviously something is not right.

int total1 = Integer.parseInt(quantity.get(0));
            int total2 = Integer.parseInt(quantity.get(1));
            int total = 0;
            for (int i = 0; i < quantity.size(); i++) {
                total = total2 + total1 ;
            }
            System.out.println(Integer.valueOf(total));

This is the Activity

public class CalculatorActivity extends AppCompatActivity {

Button btnCclcola;
TextView cancel, doneBtn, nameTV, quantityTV, resulttry;
EditText ingrName, ingrQuantity;
Dialog dialog;
RecyclerView recyclerView;
ArrayList<String> text = new ArrayList<>();
ArrayList<String> quantity = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calculator);

    nameTV = findViewById(R.id.nameTV);
    quantityTV = findViewById(R.id.qunatityTV);
    resulttry = findViewById(R.id.resulttry);
    btnCclcola = findViewById(R.id.btn_calcola);

    recyclerView = findViewById(R.id.rv_calculator);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    CalculatorAdapter calculatorAdapter = new CalculatorAdapter(this, text, quantity);
    recyclerView.setAdapter(calculatorAdapter);

    btnCclcola.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            int total1 = Integer.parseInt(quantity.get(0));
            int total2 = Integer.parseInt(quantity.get(1));
            int total = 0;
            for (int i = 0; i < quantity.size(); i++) {
                total = total2 + total1 ;
            }
            System.out.println(Integer.valueOf(total));
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {...}
   

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {...}
    

private void opendialog() {...}


private void validateCategory() {...}
   

private void saveInfo() {
    String ingName = ingrName.getText().toString();
    int ingQuant = Integer.valueOf(ingrQuantity.getText().toString());
    
    text.add(ingName);
    quantity.add(String.valueOf(Integer.valueOf(ingQuant)));

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森罗 2025-02-06 18:07:05

您的循环计算total2 + total1对于Quantity的每个条目,但您不使用迭代值。

-api将为您提供很多帮助。
通过数量创建,将它们解析为int,然后总结

int sum = quantity.stream()
    .mapToInt(Integer::parseInt)
    .sum();

Your loop computes total2 + total1 for every entry of quantity but you don't use the iteration values.

The Stream-API will help you a lot.
Create a Stream over quantity, parse them to int and summarize

int sum = quantity.stream()
    .mapToInt(Integer::parseInt)
    .sum();
公布 2025-02-06 18:07:05

很好。我从自己身上找到它。
正确的代码是:

            int total = 0;
            
            for (int i = 0; i < quantity.size(); i++) {
                int total2 = Integer.parseInt(quantity.get(i));
                total += total2;
                
                Toast.makeText(CalculatorActivity.this, ""+ total, Toast.LENGTH_SHORT).show();

It's fine. I find it by my self.
The correct code is:

            int total = 0;
            
            for (int i = 0; i < quantity.size(); i++) {
                int total2 = Integer.parseInt(quantity.get(i));
                total += total2;
                
                Toast.makeText(CalculatorActivity.this, ""+ total, Toast.LENGTH_SHORT).show();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文