重新认识Java(2_1)————Java的语言组成

做个小练习,求一个四位整数,各项位数之和

核心思想就是 利用整除(/) ,和 取模(%)来计算,代码如下:

public class Test1 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int x = in.nextInt();  // 4+5+3+2
		int  a ,b ,c ,d ; //a = 千位   b=百位   c=十位  d=个位
		a = x / 1000;
		b = x / 100 % 10; // x%1000/100;  (x - a*1000)/100
		c = x / 10 % 10;  //x%100/10 ; (x - a*1000-b*100)/10
		d = x % 10;
		System.out.println(a + b + c +d);
		
		int m = 10, n = 8;
	
		}
}

 

 

然后就是把输入的两个变量里面的值交换一下

public class Test2 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		
		//int n = 10 , m = 8;
		int n = in.nextInt(),m = in.nextInt();
		int temp;
		temp = n ;
		n = m ;
		m = temp;
		System.out.println("交换后"+n+","+m);
	}
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值