Python 官方文档:入门教程 => 点击学习
目录输入方法第一种输入方法:scanner第二种输入方法:JOptionPane第三种输入方法:io.Console输出方法第一种输出方法:System.out.print()第二种
import java.util.Scanner; // 导入java.util.Scanner
public class User {
public static void main(String[] args) {
var sc = new Scanner(System.in); //生成scanner对象
System.out.print("请输入你的姓名:");
String name = sc.nextLine();//输入字符串
System.out.print("请输入你的年龄:");
int age = sc.nextInt();//输入整数
System.out.println("姓名:" + name +"\n" +"年龄:" + age);
}
}
运行结果
常用的next()方法
nextInt() 输入整数
nextLine() 输入字符串
nextDouble() 输入双精度数
next() 输入字符串(以空格作为分隔符
运行结果
这种输入方法为安全输入方法,只能在cmd控制台运行,不能在idea、eclipse等1工具运行
创建User.java文件
import java.io.Console;//导入java.io.Console
public class User {
public static void main(String[] args) {
Console con = System.console();//建立安全输入
String name = con.readLine("请输入账号:");
String passworld = con.readLine("请输入密码:");
System.out.printf("账号:%s", name);
System.out.printf("%n密码:%s", passworld);
}
}
cmd进入文件所在目录,运行以下指令
java -Dfile.encoding=UTF-8 User.java
运行结果
System.out.print()不换行直接输出
public class User {
public static void main(String[] args) {
int a = 1;
int b = 2;
System.out.print(a);//print()不换行输出a的值
System.out.print(b);//print()不换行输出b的值
}
}
运行结果
12
System.out.println()换行输出
public class User {
public static void main(String[] args) {
int a = 1;
int b = 2;
System.out.println(a);//println()换行输出a的值
System.out.println(b);//println()换行输出b的值
}
}
运行结果
1
2
System.out.printf()格式化输出
public class User {
public static void main(String[] args) {
int a = 1;
int b = 2;
System.out.printf("a=%d%n",a);//printf()格式化输出
System.out.printf("b=%d",b);//printf()格式化输出
}
}
运行结果
a=1
b=2
到此这篇关于Java中输入与输出的方法总结的文章就介绍到这了,更多相关Java输入输出内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java中输入与输出的方法总结
本文链接: https://www.lsjlt.com/news/145547.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0