弱弱的问一下,请问Java的控制台程序中,从键盘给变量赋值最常用的方法是什么?(就像C语言中的scanf(),C++中的cin>>) 

解决方案 »

  1.   

    给int型变量赋值时存入的是AscII码值,而且两位数时我就更不会弄了
      

  2.   

    比如这段代码
    import java.io.IOException;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;public class YourTermProject {
      
        public static void main(String[] args) {
    int num=0;
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    try{
    num=in.read();
    }
    catch(IOException e){
    System.exit(1);
    }
    System.out.println(num);
        
        }}
    输入3,输出的却是51,输入32,却只读取‘3’
      

  3.   

    public class ConsoleInput {
      
        public static void main(String[] args) {
    int num=0;
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    try{
    num=Integer.parseInt(in.readLine());
    }
    catch(IOException e){
    System.exit(1);
    }
    System.out.println(num);
        
        }}
      

  4.   

    public static void main(String[] args) 
    args本身就是一个数组。它可以接受外来数据。
    args[0]args[n]都可以作为外来数据传入类中
      

  5.   

    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    ------------------------------------
    一句搞定
      

  6.   

    System.out.println("Please input a Number:");
    BufferedReader   br   =   new   BufferedReader(new   InputStreamReader(System.in));  
    int n =  Integer.parseInt(br.readLine());
      

  7.   

    还是用readLine(),因为read接受的是char,只有一个字符