可以用Scanner 给你看一个使用它的例子(只看其中用到Scanner的部分就可以了) 然后你再看看它的API 应该很快就能掌握个大概了import java.util.Scanner;//用贪心算法实现的背包问题 public class BagProblem ...{ public static void main(String[] args) ...{
Scanner input = new Scanner(System.in); int n = 0; System.out.println("please input the maxweight of bag");
//定义背包的最大容量maxWeight Bag bag = null; if(input.hasNextInt()) ...{ n = input.nextInt(); bag = new Bag(n); System.out.println("please input the number of things"); }
//定义物品的数量num Thing[] things = null; int thingsNum = 0; if(input.hasNextInt()) ...{ n = input.nextInt(); thingsNum = n; things = new Thing[n]; System.out.println("please input weight and value of thing " + 1); }
//定义每个物品的重量weight, 价值value int i = 0; while(input.hasNextInt()) ...{ int[] temp = new int[2]; for(int j = 0; j < 2; j++)...{ n = input.nextInt(); temp[j] = n; } things[i] = new Thing(temp[0], temp[1]);
if(i < thingsNum - 1) ...{ System.out.println("please input weight and value of thing " + (++i +1)); } else break; }
System.out.println(bag.getsMaxValue(things));
}} //背包类 class Bag ...{ //maxWeight:背包的最大容量 double maxWeight;
Bag(double mw) ...{ maxWeight = mw; }
public double getsMaxValue(Thing[] things) ...{ int num = things.length; double[] weights = new double[num]; double[] values = new double[num];
for(int i = 0; i < num; i++) ...{ weights[i] = things[i].weight; values[i] = things[i].value; }
Thing[] thingsBeenSorted = new Thing[num]; for(int i = 0; i < num; i++) ...{ thingsBeenSorted[i] = new Thing(weights[i], values[i]); } BubbleSort.bubbleSort(thingsBeenSorted);
至于InputStreamReader,****===> An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and translates them into characters according to a specified character encoding.通过这个类可以把这两种方式读取出来的信息相互切换。
import java.util.Scanner;Scanner input = new Scanner(System.in); 这样就可以了
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 可以了
我用过三种:一种: import java.util.Scanner;public class Scanner1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("what is your name?"); Scanner in = new Scanner(System.in); String name = in.nextLine(); System.out.println("How old are you?"); int age = in.nextInt(); // String message=String.format("My name is %s,age is %d",name,age); // System.out.printf(message); System.out.println("My name is " + name + ",age is " + age); }} 二种:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;public class BufReader { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String s=null;
}}三种:public class JOptionPane1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String name = JOptionPane.showInputDialog("what is your name?"); String age = JOptionPane.showInputDialog("How old are you?"); int age1 = Integer.parseInt(age); System.out.println("My name is " + name + ",age is " + age+"."); System.exit(0); }}还有一种是读取字节流
System.in 最简单的获得键盘的输入方法 还一中就是用流度 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
最简单的获得键盘的输入 : System.in;用流的方式: BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
俺也写一个 import java.io *; public class Hello{ public static void main(String[] args) throws IOException{ InputStreamReader reader = new InputStreamReader(System.in); BufferedReader input = new BufferedReader(reader); System.out.print("Enter your name:"); String name = input.readLine(); System.out.println("Hello,"+name); }}
第一种: import.java.util.*; Scanner sc = new Scanner(System.in); int i = sc.nextInt(); 第二种: import java.io.*; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 第三种: 在DOS命令下输入public static void main(String[] args){}数组args的参数,是在运行是输入的并且参数之间用空格隔开 第四种:(其实不算是输入,但我见别人用过) import javax.swing.*; JOptionPane.showInputDialog();会弹出一个DIALOG对话框,可以在里面输入字符串 (你可以在API中查上几个类的帮助文档,里面有的有具体用法)
system.in
public class BagProblem ...{ public static void main(String[] args) ...{
Scanner input = new Scanner(System.in);
int n = 0;
System.out.println("please input the maxweight of bag");
//定义背包的最大容量maxWeight
Bag bag = null;
if(input.hasNextInt()) ...{
n = input.nextInt();
bag = new Bag(n);
System.out.println("please input the number of things");
}
//定义物品的数量num
Thing[] things = null;
int thingsNum = 0;
if(input.hasNextInt()) ...{
n = input.nextInt();
thingsNum = n;
things = new Thing[n];
System.out.println("please input weight and value of thing " + 1);
}
//定义每个物品的重量weight, 价值value
int i = 0;
while(input.hasNextInt()) ...{
int[] temp = new int[2];
for(int j = 0; j < 2; j++)...{
n = input.nextInt();
temp[j] = n;
}
things[i] = new Thing(temp[0], temp[1]);
if(i < thingsNum - 1) ...{
System.out.println("please input weight and value of thing " + (++i +1));
}
else break;
}
System.out.println(bag.getsMaxValue(things));
}}
//背包类
class Bag ...{
//maxWeight:背包的最大容量
double maxWeight;
Bag(double mw) ...{
maxWeight = mw;
}
public double getsMaxValue(Thing[] things) ...{
int num = things.length;
double[] weights = new double[num];
double[] values = new double[num];
for(int i = 0; i < num; i++) ...{
weights[i] = things[i].weight;
values[i] = things[i].value;
}
Thing[] thingsBeenSorted = new Thing[num];
for(int i = 0; i < num; i++) ...{
thingsBeenSorted[i] = new Thing(weights[i], values[i]);
}
BubbleSort.bubbleSort(thingsBeenSorted);
double maxValue = 0;
int i;
for(i = 0 ; i < num; i++) ...{
if(thingsBeenSorted[i].weight > maxWeight) ...{break;}
thingsBeenSorted[i].part = 1;
maxValue += thingsBeenSorted[i].value;
maxWeight -= thingsBeenSorted[i].weight;
}
if(i < num) ...{
thingsBeenSorted[i].part = maxWeight/thingsBeenSorted[i].weight;
return maxValue += thingsBeenSorted[i].part * thingsBeenSorted[i].value;
}
return -1;
}
}
//物品类
class Thing implements Comparable ...{
//weight:物品的重量, value:物品的价值,
//part:物品被装入背包的部分占自身总体部分的百分比, vPerw:value与weight的比值
double weight, value, part, vPerw;
Thing(double w, double v) ...{ //, double p
weight = w;
value = v;
part = 0;
vPerw = v/w;
} public int compareTo(Object o) ...{
if(vPerw > ((Thing)o).vPerw) ...{return 1;}
if(vPerw < ((Thing)o).vPerw) ...{return -1;}
return 0;
}
}
//冒泡排序类(注意这里是按vPerw的大小降序排列的)
class BubbleSort ...{
static void bubbleSort(Thing[] things) ...{
Thing temp;
for(int i = things.length - 1; i >= 1; i --) ...{
for(int j = 0; j <= i - 1; j ++) ...{
if(things[j].compareTo(things[j+1]) < 0) ...{
temp = things[j];
things[j] = things[j+1];
things[j+1] = temp;
}
}
}
}
}
public class Main { public static void main(String[] args) {
byte[] readIn = new byte[50];
int count = 0;
try{
count = System.in.read(readIn);
System.out.println("you input:");
System.out.println(new String(readIn, 0, count));
}catch(Exception e){
e.printStackTrace();
}
}}
一种是以字节为单位读写的,InputStream、OutputStream(interface) 下面有很多类像FileInputStream、PipedInputStream 等皆继承了上述接口。
另外一种就是你上面提到的以unicode为单位进行读写的方法。 表现为 Reader、Writer、接口,像BufferedReader 就继承了Reader接口。
BufferedReader ****===> 在读取的时候,会先开辟一个缓冲区,把这些数据读到缓冲区,等到满了以后,就直接拿出来。
至于InputStreamReader,****===> An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and translates them into characters according to a specified character encoding.通过这个类可以把这两种方式读取出来的信息相互切换。
system.in ****===> 这是控制台输入信息时读入的管道。她继承PrintStream ,而PrintStream 类 继承了FilterOutputStream 类。这个类实现了OutputStream 接口。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
这样也行
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
name = in.readLine();
} catch (IOException e) {
}
Scanner sc=new Scanner(System.in)
String str=sc.next()//这里就可以读取从屏幕上输入的字符串了,还有其他很多类似的方法,读取不同的数据
可以看下API
public class Input{
public static void main(String[] args) throws Exception{
InputStreamReader dos=new InputStreamReader(System.in);
BufferedReader fos=new BufferedReader(dos);
System.out.println("请输入文件路径名及扩展名如c:\\abc.txt:");
String a=fos.readLine();
File f=new File(a);
System.out.println("若是追加方式请输入T或者覆盖请输入F以第一个字母为准,多输无效");
boolean c=true,d=true;
while(d){
char b=(char)dos.read();
if(b=='T')
{c=true;d=false;}
else if(b=='F')
{c=false;d=false;}
else
{ System.out.println("输入有误,请重新输入T或者F");}
}
FileOutputStream qw=new FileOutputStream(f,c);
if(f.exists())
System.out.println("输入的正确");
else System.out.println("输入的不正确");
OutputStreamWriter dis=new OutputStreamWriter(qw);
BufferedWriter fis=new BufferedWriter(dis);
System.out.println("请输入你想输入的内容,输入结束后请输入'end':");
String data,tt;
tt=fos.readLine(); while(true){
data=fos.readLine();
if(data.equals("end"))
break;
fis.write(data);
fis.newLine();
} fis.close();
}
}
然后主程序就这样写
public class class_name//
{
public static void main(String args()) throws IOExpection
{
BufferedReader buf;//申明buf为BufferedReader类的变量
String str;//申明str为string 类型的变量
... ...
buf=new BufferedReader (new InputStreamReader(System.in));//产生buf对象
str=buf.readline();//读入字符串至buf
... ... }
}
这个用之前要导个包:import java.util*;
这样就可以了
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
可以了
import java.util.Scanner;public class Scanner1 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("what is your name?");
Scanner in = new Scanner(System.in);
String name = in.nextLine();
System.out.println("How old are you?");
int age = in.nextInt();
// String message=String.format("My name is %s,age is %d",name,age);
// System.out.printf(message);
System.out.println("My name is " + name + ",age is " + age); }}
二种:import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;public class BufReader { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String s=null;
try {
s=bf.readLine();
System.out.println(s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}三种:public class JOptionPane1 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String name = JOptionPane.showInputDialog("what is your name?");
String age = JOptionPane.showInputDialog("How old are you?");
int age1 = Integer.parseInt(age);
System.out.println("My name is " + name + ",age is " + age+".");
System.exit(0); }}还有一种是读取字节流
Scanner input=new Scanner(System.in);
我就学过这样的
String youName = null; //声明String类保存信息
System.out.println("请输入您的姓名:"); //提示输入
Scanner input = new Scanner(System.in); //声明Scanner类的对象
youName = input.next(); //接受的值赋给youName变量
System.out.println("您的名字是: "+ youName); //测试输入接受键盘输入的信息
}
}
2.类对象Scanner
3.缓冲I/O Stream(输入/输出流)关于楼上说的键盘监听(KeyListener),作为新手不清楚,API上说:
用于接收键盘事件(击键)的侦听器接口。旨在处理键盘事件的类要么实现此接口(及其包含的所有方法),要么扩展抽象 KeyAdapter 类(仅重写有用的方法)。
然后使用组件的 addKeyListener 方法将从该类所创建的侦听器对象向该组件注册。按下、释放或键入键时生成键盘事件。然后调用侦听器对象中的相关方法并将该 KeyEvent 传递给它。UP
还有一种是使用 JDK 6 新增的 java.io.Console 类。具体的详见:http://blog.csdn.net/bao110908/archive/2008/02/20/2108930.aspx
还一中就是用流度
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
最简单的获得键盘的输入 :
System.in;用流的方式:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
import java.io *;
public class Hello{
public static void main(String[] args) throws IOException{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.print("Enter your name:");
String name = input.readLine();
System.out.println("Hello,"+name);
}}
import.java.util.*;
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
第二种:
import java.io.*;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
第三种:
在DOS命令下输入public static void main(String[] args){}数组args的参数,是在运行是输入的并且参数之间用空格隔开
第四种:(其实不算是输入,但我见别人用过)
import javax.swing.*;
JOptionPane.showInputDialog();会弹出一个DIALOG对话框,可以在里面输入字符串
(你可以在API中查上几个类的帮助文档,里面有的有具体用法)