如何在同一println"System.out.println"文件中打印这2个变量? [英] How can I print this 2 Variables in the same println "System.out.println"
本文介绍了如何在同一println"System.out.println"文件中打印这2个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个 int
变量 abdou1
和 abdou2
,我希望打印这些变量的值.我在下面尝试了无效的方法.
I have two int
variables, abdou1
and abdou2
, I wish to print the values of these. I have tried below which does not work.
public class Math1 {
public static void main (String args[]) {
int abdou1 = 115;
double abdou2 = 1122.176876;
System.out.println(abdou1, abdou2);
}
}
推荐答案
由于Java中的printf方法的工作方式与C printf函数相同,因此必须在此处使用格式说明符来标识数据类型.
As printf method in java works same like C printf function so have to use format specifiers here to identify the data type.
public class Math1 {
public static void main (String args[]) {
int abdou1 = 115;
double abdou2 = 1122.176876;
System.out.println(String.format("%d %f", abdou1, abdou2));
}
您可以将这些格式说明符用于不同的数据类型
You can use these Format Specifiers for different data types
- %c或%C显示字符
- %d显示一个十进制(以10为底)的整数
- %e或%E以指数表示法显示浮点数
- %f以十进制格式显示浮点值
- %s或%S显示字符串
- %b或%B显示布尔值
- %g(%G)浮动或根据需要重复使用%f或%e
- %o int无符号八进制值
- %p指针地址存储在指针中
- %s个字符或字符串的char序列数组
- %u int无符号十进制
- %x(%X)int无符号十六进制值
- %%显示一个%符号
您可以使用
- 空格('')
- 标签('\ t')
- 回车('\ r')
- 换行符('\ n')
- ormfeed('\ f')
有关其他数据类型的更多详细说明和示例,请访问此链接.
格式说明符
For more further explanation and examples with other data types you can go through this link.
Format Specifiers
这篇关于如何在同一println"System.out.println"文件中打印这2个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文