Java 中如何输入

JAVA中怎样输入字符串~

1.首先,导入java.util.*包。

2.然后,你需要新建一个读取标准输入(键盘)的扫描器对象。

3.现在,你可以从键盘输入字符串了。

扩展资料:
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 。
Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。
Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。
参考资料:java-百度百科

import java.util.*;
public class Test_01
{
public static void main(String[] args)throws Exception
{
System.out.println("请输入一个字符");
char c=(char)System.in.read();
System.out.println(c);
}
}

扩展资料:还可以输入字符串,输入字符串的方法
import java.io.*;
public class Test
{
public static void main(String[] args) throws IOException
{
BufferedReader buf = new BufferedReader (new InputStreamReader(System.in));
BufferedWriter buff = new BufferedWriter(new FileWriter("abc.txt"));
String str = buf.readLine();
while(!str.equals("exit"))
{
buff.write(str);
buff.newLine();
str = buf.readLine();
}
buf.close();
buff.close();
}
}

import java.util.Scanner; //键盘扫描类
public class Test{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);//创建一个键盘扫描类对象
System.out.print("请您输入内容:");
int contents=input.nextInt(); //输入整型
String contents1=input.next(); //输入字符串型
区别是在next后面的数据类型String在next后面是省略了。

现提供两种方法:
1.用输入输出。引用:
import java.io.BufferedReader;
import java.io.InputStreamReader;
具体应用:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// 从控制台读数据。
try {
str = br.readLine();
}catch(IOException e) {
e.printStackTrace();
}
此时,str存有你输入的数据。

2.利用util中的Scanner。
Scanner sc = new Scanner(System.in);
int input =sc.nextInt();
input 存有你输入的整数。

PS;受整型 字符型 字符串,相关请查API文档。

Java中做输入的方式:
通过控制台输入数据,需要使用Scanner对象来操作,如下:
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.println(a);

可以作为参数传进,也可以用InputStream。输入的是字节流或字符流,再自己转换类型。请查java doc中java.io一节。

System.in.read();

相关兴趣推荐

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网