[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
题目:给你一个整数 n,请你帮忙计算并返回该整数「各位数字之积」与「各位数字之和」的差。
这是最终代码:
import java.util.Scanner; public class One { public static void main(String[]
args) { Scanner sc=new Scanner(System.in); System.out.println("请输入n的值"); int
a=sc.nextInt(); System.out.println(getAdd(a)); } public static int getAdd(int
n) { int muti=1; int add=0; while(n!=0) { muti*=n%10; add+=n%10; n=n/10; }
return (muti-add); } }
用while循环,对10取余,得到每一位数字再去依次和muti,add相乘和相加。
而我的初步思路是这样:定义方法,它不是每一位的相加或者相乘,就先把每位数字算出来,再这些数字分别相加乘,减,然后调用。
所以就有了我这种啰嗦的做法而且还不对。
复杂性如下:
问题
(1)因为不能确定他就只会执行ge下面的不执行,
(2)一开始就把位数定了;
这里名词解释一下:
public:权限修饰符,就是公开,所有方法都可调用;
static:静态修饰符;
getAdd:方法名;
形参:方法定义时的参数,只是个形式,任意取;
实参:调方法的后面跟的参数,比如(getAdd(a)这个a 就是实参;
实参的值赋给形参让他去执行自己的功能块。