日期:2014-05-20  浏览次数:20656 次

关于Java中的输入问题……
Java   中有没有像C中那样的输入函数如scanf直接可从键盘输入数据?
在JAVA中怎么样通过键盘输入生成一个数组,并向其中放入数据元素?
大家帮帮忙阿……要交作业,十万火急阿!

------解决方案--------------------
args....
------解决方案--------------------
public static void main(String[] args)
{
String[] s = args;
for(int i=0; i < s.length;i++){
System.out.println(s[i]);
}
}
------解决方案--------------------
System.in
------解决方案--------------------
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String temp_str = input.readLine();
------解决方案--------------------
scanner~
------解决方案--------------------
int array[]=new int[20];
for(int i=0;i <array.length;i++)
{
s=JOptionPane.showInputDialog( "请输入第 "+(i+1)+ "个数: ");
t=Integer.parseInt(s);
array[i]=t;
}
------解决方案--------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class inputSample {

/**
* @param args
*/
public static void main(String[] args) {

String[] sName = new String[10];
System.out.println( "please input user-name ");
int i = 0;
for(; i < 10 ; i++)
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader( isr );
try {
sName[i] = br.readLine();
} catch (IOException e) {

e.printStackTrace();
}

}

for(i = 0; i <10; i++)
System.out.println( "arr[ "+i+ "]= "+sName[i]);



}

}

------解决方案--------------------
System.out System.in
------解决方案--------------------
好像用 Scanner类和System.in结合可以读入数据
------解决方案--------------------
Scanner r=new Scanner(System.in);
String s=r.nextLine();

------解决方案--------------------
JDK 1.5增加了一个类 可以办到

import java.util.*
public class Name
{
public static void main(String args[])
{
Scanner R=new Scanner(System.in);
//然后用R来调用一些函数就可以了,具体函数如下
}
}

nextInt()//输入整数
hasNextInt()//判断输入的是否是整数 是==true
类似还有:
nextByte() hasNextByte()
nextDouble() hasNextDouble()
nextFloat() hasNextFloat()
nextLine() hasNextLine() //可以得到一个String 类型的
nextLong() hasNextLong()
nextShort() hasNextShort()