java练习!java练习

输入一个正整数,每位非零数之积,例如:输入“1203”,输出“6”

img


package com;

public class Main {

    public static void main(String[] args) {
        String s = "1203";
        int res = 1;

        for (char c : s.toCharArray()) {
            if (c != '0') {
                res *= Integer.valueOf(c + "");
            }
        }
        System.out.println(res);
    }

}


import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();//输入
        int k;//每一位数字
        int s = 1;//乘积
        while (n > 0) {
            k = n % 10;
            if (k > 0) {
                s = s * k;
            }
            n = n / 10;
        }
        System.out.println(s);
    }
}

如有帮助,望采纳

class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要计算的数字");
        String string = scanner.nextLine();
        System.out.println(mathTest(string));;
    }
    public static Integer   mathTest(String string){
        System.out.println(string);
        int s=1;
        for (int i=0;i<string.length();i++){
            if (string.charAt(i)!='0'){
//                获得字符串中的对应下标的对应数字值
                System.out.println(Integer.parseInt(String.valueOf(string.charAt(i))));;
//                将值与上次的值相乘
                s=s*Integer.parseInt(String.valueOf(string.charAt(i)));
            }
        }
//返回计算结果
        return  s;
    
    }
}