I need help to sum all the integers in the arrayList and view the result of the sum in the text view, all the eight number is from edit text here is the full code that get the integer from edit text and view the sum result on the text view
我需要帮助来汇总arrayList中的所有整数并在文本视图中查看和的结果,所有八个数字来自编辑文本,这里是从编辑文本中获取整数的完整代码,并查看总和结果文本视图
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
TextView textView;
int one, two, three, four, five, six, seven, eight;
int Result;
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.text);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String data = editText.getText().toString();
one = Integer.parseInt(data.charAt(0) + "");
two = Integer.parseInt(data.charAt(1) + "");
three = Integer.parseInt(data.charAt(2) + "");
four = Integer.parseInt(data.charAt(3) + "");
five = Integer.parseInt(data.charAt(4) + "");
six = Integer.parseInt(data.charAt(5) + "");
seven = Integer.parseInt(data.charAt(6) + "");
eight = Integer.parseInt(data.charAt(7) + "");
int[] mNumber = {one, two * 2, three, four * 2, five, six * 2, seven, eight * 2};
for (int n : mNumber) {
spiltInt(n);
}
}
});
}
public void spiltInt(int x) {
String number = String.valueOf(x);
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < number.length(); i++) {
int j = Character.digit(number.charAt(i), 10);
list.add(j);
}
}
}
3 个解决方案
#1
0
You can do this
你可以这样做
//Assumig variables one,two,three,four,five,six are defined above as int
int[] mNumber = {one, two * 2, three, four * 2, five, six * 2, seven, eight * 2};
for (int n : mNumber) {
spiltInt(n);
}
showSum(mNumber);
public void spiltInt(int x) {
String number = String.valueOf(x);
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < number.length(); i++) {
int j = Character.digit(number.charAt(i), 10);
list.add(j);
}
int result = 0;
for(Integer i:list){
result+=i;
}
textView.setText(result+"");
}
public static void showSum(int[] array){
int sum = 0;
int digitSum = 0;
for(int i=0;i<array.length;i++){
sum+=array[i];
String number = String.valueOf(array[i]);
ArrayList<Integer> list = new ArrayList<>();
for (int j = 0; j < number.length(); j++) {
int k = Character.digit(number.charAt(j), 10);
list.add(k);
}
for(Integer digit:list){
digitSum+=digit;
}
}
//you can set any of these into test view using setText method
System.out.println(sum+"");
System.out.println(digitSum+"");
}
#2
3
Assuming you want to sum all the digits from an array of integers, here's a 1-liner:
假设您想要对整数数组中的所有数字求和,这里是一个1-liner:
int digitSum = IntStream.of(numbers).flatMap(n -> ("" + n).chars()).map(i -> i - '0').sum();
#3
0
You are calling spiltInt()
which is a non static
method and it cannot be referenced from a static context.
您正在调用spiltInt()这是一个非静态方法,它不能从静态上下文中引用。
Just make you spiltInt()
as static
and call it using the class name which is MainActivity
in this case.
只需将spiltInt()设为静态,并使用类名称调用它,在本例中为MainActivity。
your code becomes
你的代码变成了
for (int n : mNumber) {
MainActivity.spiltInt(n);
}
textView.setText(result+"");
Now, to print the result make result
variable global.
现在,打印结果使结果变量全局。
and use the following code at the end of your spiltInt()
code
并在spiltInt()代码的末尾使用以下代码
like
for(int k : list){
result = result+k;
}
#1
0
You can do this
你可以这样做
//Assumig variables one,two,three,four,five,six are defined above as int
int[] mNumber = {one, two * 2, three, four * 2, five, six * 2, seven, eight * 2};
for (int n : mNumber) {
spiltInt(n);
}
showSum(mNumber);
public void spiltInt(int x) {
String number = String.valueOf(x);
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < number.length(); i++) {
int j = Character.digit(number.charAt(i), 10);
list.add(j);
}
int result = 0;
for(Integer i:list){
result+=i;
}
textView.setText(result+"");
}
public static void showSum(int[] array){
int sum = 0;
int digitSum = 0;
for(int i=0;i<array.length;i++){
sum+=array[i];
String number = String.valueOf(array[i]);
ArrayList<Integer> list = new ArrayList<>();
for (int j = 0; j < number.length(); j++) {
int k = Character.digit(number.charAt(j), 10);
list.add(k);
}
for(Integer digit:list){
digitSum+=digit;
}
}
//you can set any of these into test view using setText method
System.out.println(sum+"");
System.out.println(digitSum+"");
}
#2
3
Assuming you want to sum all the digits from an array of integers, here's a 1-liner:
假设您想要对整数数组中的所有数字求和,这里是一个1-liner:
int digitSum = IntStream.of(numbers).flatMap(n -> ("" + n).chars()).map(i -> i - '0').sum();
#3
0
You are calling spiltInt()
which is a non static
method and it cannot be referenced from a static context.
您正在调用spiltInt()这是一个非静态方法,它不能从静态上下文中引用。
Just make you spiltInt()
as static
and call it using the class name which is MainActivity
in this case.
只需将spiltInt()设为静态,并使用类名称调用它,在本例中为MainActivity。
your code becomes
你的代码变成了
for (int n : mNumber) {
MainActivity.spiltInt(n);
}
textView.setText(result+"");
Now, to print the result make result
variable global.
现在,打印结果使结果变量全局。
and use the following code at the end of your spiltInt()
code
并在spiltInt()代码的末尾使用以下代码
like
for(int k : list){
result = result+k;
}