zoukankan      html  css  js  c++  java
  • 这么简单错两次 菜的出奇

    编写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大写

  • 相关阅读:
    python pandas写入excel文件
    Ubuntu Teamviewer安装使用
    Ubuntu18.04 有线无法正常上网(请读完全文再进行操作)
    2019/4/5 python正则表达式的中文文档
    2019/4/3 Python今日收获
    2019/3/28 Python今日收获
    2019/3/15 Python今日收获
    2019/3/9 Python今日收获
    2019/2/26 Python今日收获
    2019/2/19 Python今日收获
  • 原文地址:https://www.cnblogs.com/husiyu/p/12452076.html
Copyright © 2011-2022 走看看