java中%号相关

在java学习过程中发现了%相关内容
在这里插入图片描述
学习所用代码:

public class HelloWorld {
    public static void main(String []args) {
       double x = 11.635;
       double y = 2.76;
        System.out.printf("e 的值为 %1f%n", Math.E);
        System.out.printf("exp(%.3f) 为 %.3f%n", x, Math.exp(x));
		 System.out.printf("exp(%.3f) 为 %.3f%n", x, Math.exp(2));
		//%n表示换行
		 System.out.printf("exp(%.3f) 为 %.3f", x, Math.exp(2));
		//double c = 78953368732098.38;
		double c = 11;
		//infinity 无穷大
	    System.out.printf("exp(%.3f) 为 %.3f%n",c, Math.exp(c));
    }
}

输出

e 的值为 2.718282
exp(11.635)112983.831
exp(11.635)7.389
exp(11.635)7.389exp(11.000)59874.142

exp函数相关解释
在这里插入图片描述

————————————————————————————————————————————

%n  换行  相当于 \n

%c  单个字符

%d  十进制整数

%u  无符号十进制数

%f  十进制浮点数

%o  八进制数

%x  十六进制数

%s  字符串

%%  输出百分号

%.4f      四位小数的十进制浮点数

实例

String name = “花花”;
System.out.printf(“你好,我的名字叫%s”, name);//“你好,我的名字叫花花”
int num = 004;
System.out.printf(“%s的学号是%d”, name, num);//“花花的学号是004”
如上所示,printf方法接收n个参数,第一个参数为你想要输出的字符串,第二个参数为第一个参数中的第一个格式(%s等),第三个参数为第一个参数中第二个格式(%s等),以此类推。

实例

public class Test{
    public static void main(String args[]){
        double x = 11.635;
        double y = 2.76;

        System.out.printf("e 的值为 %.4f%n", Math.E);
        System.out.printf("pow(%.3f, %.3f) 为 %.3f%n", x, y, Math.pow(x, y));
    }
}

以上实例输出结果为:

e 的值为 2.7183
pow(11.635, 2.760)874.008
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值