学习java第五天

水仙花代码小练

水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。

package shuixcianhua;
public class shuixian {
public static void main(String []args){
	int num=0;
	for(num=0;num<1000;num++){
	int ones=num%10;
	int tens=(num%100)/10;
	int units=num/100;
	if(num==ones*ones*ones+tens*tens*tens+units*units*units){
	System.out.println(num);
	}
	}
}	
}

喝酒逢7过小游戏

这里定义的数字范围大小为0-99

有7或者是7的倍数都要接受惩罚,这里把0-99的特征数字都罗列出来。

package shuixcianhua;
public class shuixian {
public static void main(String []args){
	int num=0;
	for(num=0;num<100;num++){
	int ones=num%10;
	int tens=num/10;
if(num%7==0||ones==7||tens==7)
	System.out.println(num);
	}
}	}

输入数字倒序输出

这个限制太多了,有很多瑕疵的地方,下面我将尝试写一个能倒叙输出字符的且没有长度限制(这里没有限制一般是指大于9999)

package shuixcianhua;
import java.util.*;
public class shuixian {
public static void main(String []args){
	Scanner sc = new Scanner(System.in);
	System.out.println("请输入一串数字");
	int num=sc.nextInt();
	while(num!=0){
		int code=num%10;
		num=num/10;
		System.out.print(code);
		
	}
}	}
请输入一串数字
123654789
987456321

倒叙输出字符

package shuixcianhua;
import java.util.*;
public class shuixian {
public static void main(String []args){
	Scanner sc = new Scanner(System.in);
	System.out.println("请输入一串字符");
    String array;
	array = sc.next();
	for ( int i = array.length()-1;i>=0;i--){
		System.out.print(array.charAt(i));

	}
}	}

这里比较重要的是

for ( int i = array.length()-1;i>=0;i--)

这里的倒叙输出的要从array的数组长度减1开始,到零结束。例如0-9有十个字符,在字符数组中想要倒叙输出,要从a[9]开始-a[0]结束,但是a.length()=10。

99乘法表

package shuixcianhua;
import java.util.*;
public class shuixian {
public static void main(String []args){
	int num=0;
	for(int high=1;high<10;high++){
		for(int wide=1;wide<=high;wide++){
			System.out.printf(high+"*"+wide+"="+high*wide+"  ");
		}
		System.out.println();
	}
}	}

1*1=1  
2*1=2  2*2=4  
3*1=3  3*2=6  3*3=9  
4*1=4  4*2=8  4*3=12  4*4=16  
5*1=5  5*2=10  5*3=15  5*4=20  5*5=25  
6*1=6  6*2=12  6*3=18  6*4=24  6*5=30  6*6=36  
7*1=7  7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49  
8*1=8  8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64  
9*1=9  9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81  

这里其实不太美观,从第四行开始就有点断层,并且竖着看左边的数值应该不变,这里左边的数字变化了,有点不好看。但是我懒,而且这也代码一点也不冗余,清清爽爽。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值