import java.io.*;
class MA
{ public static void main(String args[])
throws IOException
{ int a,b,c;
BufferedReader ch=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入a的值:");
a=ch.read();
System.out.println("请输入b的值:");
b=ch.read();
c=a+b;
System.out.println("c的值为:"+c);
}
}
class MA
{ public static void main(String args[])
throws IOException
{ int a,b,c;
BufferedReader ch=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入a的值:");
a=ch.read();
System.out.println("请输入b的值:");
b=ch.read();
c=a+b;
System.out.println("c的值为:"+c);
}
}
import java.io.*;
class MA
{ public static void main(String args[])
throws Exception
{ String a,b;
int c;
BufferedReader ch=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入a的值:");
a=ch.readLine();
System.out.println("请输入b的值:");
b=ch.readLine();
c=Integer.parseInt(a)+Integer.parseInt(b);
System.out.println("c的值为:"+c);
}
}
一下摘自JAVADOC:public final class Scanner
extends Object
implements Iterator<String>一个可以使用正则表达式来分析基本类型和字符串的简单文本扫描器。Scanner 使用分隔符模式将其输入分解为标记,默认情况下该分隔符模式与空白匹配。然后可以使用不同的 next 方法将得到的标记转换为不同类型的值。例如,以下代码使用户能够从 System.in 中读取一个数: Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
再看一个例子,以下代码使 long 类型可以通过 myNumbers 文件中的项分配: Scanner sc = new Scanner(new File("myNumbers"));
while (sc.hasNextLong()) {
long aLong = sc.nextLong();
}扫描器还可以使用不同于空白的分隔符。下面是从一个字符串读取若干项的例子: String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close(); 输出为: 1
2
red
blue 以下代码使用正则表达式同时分析所有的 4 个标记,并可以产生与上例相同的输出结果: String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input);
s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
MatchResult result = s.match();
for (int i=1; i<=result.groupCount(); i++)
System.out.println(result.group(i);
s.close(); 扫描器所使用的默认空白分隔符通过 Character.isWhitespace 来识别。扫描操作可能被阻塞,而等待信息的输入。
Integer.parseInt();
最后相加