Live2D

这么简单错两次 菜的出奇

编写Java程序,求13-23+33-43+…+973-983+993-1003的值。

第一次:

public class jiafa{
	public static void main(String[] args){
		int i,sum=0;
		int flag=1;
		for(i=13;i<=1003;i+=10){
			sum+=(flag*i);
			flag*=-1;
			
		}
		System.out.println(sum);
	}
}

虽然编译器没有报错,不过我是真的菜的真实,事实证明平时代码不能写的太嗨,会飘!!!

PS:
1.虽然每次flag自乘一个-1改变符号,可是这个-23没有在for循环的范围内。。。
不对,我好像对了哈哈

第二次 打印输出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其中各位数字立方和等于该数本身。例如,153是一个“水仙花数”。

public class shuixianhua {

	public static void main(String args[]) {
		// TODO Auto-generated method stub
		int a,b,c,i,n;
		a=n%10;
		b=n/10%10;
		c=n/100%10;
		if(n==Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3)) {
			System.out.println(n);
		}
		}
}
}

蜜汁操作

错误:1.没有把三位数这个得分点表示出来
2.n来的莫名其妙,没有得到具体n的初始化

代码改正:

public class shuixianhua {

	public static void main(String args[]) {
		// TODO Auto-generated method stub
		int a,b,c,i;
		for(int n=100;n<=999;n++){
		a=n%10;
		b=n/10%10;
		c=n/100%10;
		if(n==Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3)) {
			System.out.println(n);
		}
		}
}
}

收获:

Java的立方格式为Math.pow(底数,次方数); PS:MathM大写

posted @ 2020-03-09 22:24  饭耶  阅读(207)  评论(0编辑  收藏  举报