一、基本程序框架
import java.util.Scanner;//明确导入——导入指定的类(从包 java.util中导入Scanner类) import java.util.*;//通配符导入——导入一个包(java.util)中的所有类 public class JavaStudy {//类名 public static void main(String[] args) { Scanner cin = new Scanner(System.in);//使得可以从控制台获取输入(知道即可) int a = cin.nextInt();//调用Scanner对象cin的nextInt()方法完成从控制台输入一个int值 System.out.println(a);//调用System.out的println()方法完成从控制台输出 } }
注:使用 Scanner cin = new Scanner(new BufferedInputStream(System.in)); 定义的cin进行输入时可能会更快
二、常见数据类型及输入
import java.util.Scanner; public class JavaStudy { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int a = cin.nextInt();//输入一个整数 System.out.println(a);//相当于C++中的cout<<a<<endl; double b = cin.nextDouble();//输入一个浮点数 System.out.println(b); String s = cin.next();//输入一个字符串,不读入空格(即以空格作为分界符) System.out.println(s); //String ss = cin.nextLine();//输入一个整行,读入空格,不读入回车(即以回车作为分界符),一般不与上述方法混合使用 //System.out.print(ss);//相当于C++中的cout<<ss;
//while(cin.hasNext()){ }相当于!=EOF } }
运行结果:
(绿字为输入,黑字为输出)
注:Java中数组,字符串转字符数组将在后续补充
三、基本输出
import java.util.Scanner; import java.text.DecimalFormat; public class JavaStudy { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int a = cin.nextInt(); System.out.println(a);//相当于C++中的cout<<a<<endl; System.out.print(a);//相当于C++中的cout<<a; System.out.printf("%5d ",a);//相当于C中的printf("%5d ",a); double b = cin.nextDouble(); DecimalFormat f1 = new DecimalFormat("0.00");//导入DecimalFormat类 System.out.println("b = " + f1.format(b));//‘+’表示字符串的连接,相当于C中的printf("b = %.2f ",b);四舍五入 DecimalFormat f2 = new DecimalFormat("0.0#");//‘#’表示若最后一位为0则不输出,其它位置的‘#’也有类似效果 b = cin.nextDouble(); System.out.println("b = " + f2.format(b)); b = cin.nextDouble(); System.out.println("b = " + f2.format(b)); } }
运行结果:
四、Java中BigInteger类的使用
1.BigInteger的定义及赋值
import java.util.Scanner; import java.math.BigInteger; public class JavaStudy { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int a = 666; BigInteger aa = BigInteger.valueOf(a);//相当于aa = BigInteger.valueof(666);将数值转为大数 System.out.println("aa = " + aa); BigInteger bb = new BigInteger("1234");//用字符串“1234”构造大数 System.out.println("bb = " + bb); String s = "12345678987654321"; BigInteger ss = new BigInteger(s);//用字符串构造大数 System.out.println("ss = " + ss); BigInteger cc = new BigInteger("1010",2);//将字符串“1010”视为二进制的1010来构造大数 System.out.println("cc = " + cc); String tmp = ss.toString();//将大数转为字符串 System.out.println("tmp = " + tmp); } }
运行结果:
2.BigInteger类的方法
import java.util.Scanner; import java.math.BigInteger; public class JavaStudy { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int a = cin.nextInt(); int b = cin.nextInt(); BigInteger aa = BigInteger.valueOf(a); BigInteger bb = BigInteger.valueOf(b); BigInteger cc = aa.add(bb);//加,aa,bb本身的值不改变 System.out.println("aa + bb = " + cc); BigInteger dd = aa.subtract(bb);//减 System.out.println("aa - bb = " + dd); BigInteger ee = aa.multiply(bb);//乘 System.out.println("aa * bb = " + ee); BigInteger ff = aa.divide(bb);//除 System.out.println("aa / bb = " + ff); BigInteger gg = aa.mod(bb);//模 System.out.println("aa mod bb = " + gg); int tmp = 3; BigInteger hh = aa.pow(tmp);//幂 System.out.println("aa ^ tmp = " + hh); BigInteger ii = aa.abs();//绝对值 System.out.println("abs(aa) = " + ii); BigInteger jj = aa.negate();//取反 System.out.println("-aa = " + jj); BigInteger kk = aa.min(bb); BigInteger ll = aa.max(bb);//取最值 System.out.println("min(aa,bb) = " + kk + " " + "max(aa,bb) = " + ll); int cmp = aa.compareTo(bb);//比较 System.out.println("cmp(aa,bb) = " + cmp); boolean isequal = aa.equals(bb);//判断是否相等 System.out.println("isequal(aa,bb) = " + isequal); BigInteger mm = aa.gcd(bb);//求最大公约数 System.out.println("gcd(aa,bb) = " + mm); } }
运行结果:
五、程序示例 hdu1002 A+B Problem Ⅱ
import java.util.Scanner; import java.math.BigInteger; public class Main{//提交的程序中有且仅有一个名为Main的类 public static void main(String[] args) { Scanner cin = new Scanner(System.in); int t = cin.nextInt(); for(int i=1;i<=t;i++) { String a = cin.next(); String b = cin.next(); BigInteger aa =new BigInteger(a); BigInteger bb = new BigInteger(b); System.out.println("Case " + i + ":"); System.out.println(aa + " + " + bb + " = " + aa.add(bb)); if(i!=t) System.out.println(); } } }
六、待续:数组、排序