代码清单2-9所示的程序会读取通过键盘输人的两个整数值,并对这两个值进行加减乘除运算,然后显示运算结果。 |
查看代码
public class Main1{
public static void main(String[]args){
System.out.println("对x和y进行加减乘除运算。");
int x=7;
System.out.println("x的值:"+x);
int y=5;
System.out.println("y的值:"+y);
System.out.println("x + y="+(x+y));
System.out.println("x - y="+(x-y));
System.out.println("x * y="+(x*y));
System.out.println("x / y="+(x/y));
System.out.println("x % y="+(x%y));
}
}
取得相反数 |
查看代码
import java.util.Scanner;
public class Main1{
public static void main (String[] args) {
Scanner stdIn = new Scanner (System. in) ;
System.out.print("整数值: ") ;
int a = stdIn.nextInt();
int b=-a;
System.out.println(a + "的符号取反后的值是"+ b+".");
}
}
字符串读入 |
查看代码
import java.util.Scanner;
public class Main1{
public static void main (String[] args) {
Scanner stdIn = new Scanner (System. in) ;
System.out.println(stdIn.next());
}
}