在java中乘以arraylist值

我输入一个像 1234 这样的数字。我需要偶数位置值和奇数位置值,并且我在 arraylist 中存储了偶数位置 arraylist 值是 2 和 4。在奇数 arraylist 值是 1 和 3 它都工作正常。但是当我乘法时2 和 4 的 arraylist 值我得到 2600 。请帮忙


import java.util.*;

public class list {



public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        List<Character> list1 = new ArrayList<>();

        List<Character> list2 = new ArrayList<>();

        System.out.print("Enter Distance ");

        String no = sc.next();

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

            if(i % 2 != 0){

                list1.add(no.charAt(i));

            }else{

                list2.add(no.charAt(i));

            }

        }


          for (char c : list1 ) {

            System.out.println(c);

          }


        int tot = 1;

        for (int i=0; i < list1.size() ; i++ ) {

            tot = tot * list1.get(i);

        }


        System.out.print(tot);



    }

}


天涯尽头无女友
浏览 168回答 3
3回答

守着一只汪

你乘以字符而不是数字,这就是你得到 2600 的原因。在将它们相乘之前将你的字符转换为数字。这是更新的代码。import java.util.*;public class Main{&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> list1 = new ArrayList<>();//changed here&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> list2 = new ArrayList<>();//changed here&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter Distance ");&nbsp; &nbsp; &nbsp; &nbsp; String no = sc.next();&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Integer.parseInt(no);&nbsp; &nbsp; &nbsp; &nbsp; }catch(Exception e ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("NumberFormatException");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0 ; i < no.length() ; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i % 2 != 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list1.add(Character.getNumericValue(no.charAt(i)));//changed here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list2.add(Character.getNumericValue(no.charAt(i)));//changed here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int c : list1 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(c);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; int tot = 1;&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i < list1.size() ; i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tot = tot * list1.get(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(tot);&nbsp; &nbsp; }}

慕莱坞森

您正在将Characters 与 h相乘int。所以字符会自动转换为整数,但 java 获取这些字符的 ASCII 值(例如 '0' == 48)。因为 '2' 的 ASCII 值是 50 作为整数,而 '4' 的值是 52 作为整数,所以当它们相乘时你得到 2600。您可以通过替换“0”值来简单地将 ASCII 值转换为整数值:tot = tot * (list1.get(i) - '0');你可以使用 java 8 stream API 来做你想做的事:int tot = no.chars() // Transform the no String into IntStream&nbsp; &nbsp; &nbsp; &nbsp; .map(Integer.valueOf(String.valueOf((char) i))) // Transform the letter ASCII value into integer&nbsp; &nbsp; &nbsp; &nbsp; .filter(i -> i % 2 == 0) // remove all odd number&nbsp; &nbsp; &nbsp; &nbsp; .peek(System.out::println) // print remaining elements&nbsp; &nbsp; &nbsp; &nbsp; .reduce(1, (i, j) -> i * j); // multiply all element of the list (with the first value of 1)

慕妹3146593

您应该将字符转换为整数值:for (int i=0; i < list1.size() ; i++ ) {&nbsp; tot = tot *&nbsp; Integer.valueOf(list1.get(i).toString());}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java