从用户编号输入中获取总和
我正在尝试从所有用户数量输入中获得一笔汇率。 用户从对话框中输入数量号。该数量是在阵列列表中的文本视图中显示的。 在阵列数量中,我可以获得位置(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的循环计算
total2 + total1
对于Quantity
的每个条目,但您不使用迭代值。流
-api将为您提供很多帮助。通过
数量创建
流
,将它们解析为int
,然后总结Your loop computes
total2 + total1
for every entry ofquantity
but you don't use the iteration values.The
Stream
-API will help you a lot.Create a
Stream
overquantity
, parse them toint
and summarize很好。我从自己身上找到它。
正确的代码是:
It's fine. I find it by my self.
The correct code is: