JAVA 知识记录

再次复习一下JAVA知识 ,同时记录一下

JAVA中创建除主函数之外的函数 (在原先的class 或者 另外创建class 都可行)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package ddd;
import java.io.*;
import java.io.BufferedInputStream;
import java.math.*;
import java.util.*;
public class Main
{
public static void main( String[] args) {
Scanner cin = new Scanner(System.in);
int a,b;
a = cin.nextInt();
b = cin.nextInt();
System.out.println(calculate.add(a, b));
System.out.println(calculate.sub(a, b));
System.out.println(mul(a,b));
}
public static int mul(int a,int b)
{
return a * b;
}
}
class calculate {
public static int add(int a,int b)
{
return a + b;
}
public static int sub(int a,int b)
{
return a - b;
}
}
求出一个数除以另一个数的商以及余数。
1
2
3
4
5
6
7
8
9
10
11
12
public class Main
{
public static void main( String[] args) {
Scanner cin = new Scanner(System.in);
BigInteger a,b;
a = cin.nextBigInteger();
b = cin.nextBigInteger();
BigInteger result[] = a.divideAndRemainder(b);
System.out.println(result[0]);
System.out.println(result[1]);
}
}
对字符串的修改。 String 本身是不可以修改的,但是可以通过转换为字符数组进行修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Main
{
public static void main( String[] args) {
Scanner cin = new Scanner(System.in);
int i;
String s = "abcdefg";
System.out.println(s.charAt(0));
char [] ch;
ch = s.toCharArray();
for(i = 0; i < ch.length; ++i)
{
ch[i] += 1;
}
System.out.println(ch);
}
}
将int型强制转化为BigInteger类型 以及 .compareTo 函数的使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Main
{
public static void main( String[] args) {
Scanner cin = new Scanner(System.in);
int a = 10;
int b = 11;
BigInteger c ,d;
c = BigInteger.valueOf(a);
d = BigInteger.valueOf(b);
if(c.compareTo(d) == 0)
System.out.println(1111);
else
System.out.println(2222);
}
}
JAVA 中的进制转换
1
2
3
4
5
6
7
8
9
10
11
12
13
//String st = Integer.toString(num, base);
//把num 当做10进制的数转成base进制的(base <= 35)
public class Main
{
public static void main( String[] args) {
Scanner cin = new Scanner(System.in);
int num,base;
num = cin.nextInt();
base = cin.nextInt();
String st = Integer.toString(num,base);
System.out.println(st);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
// int num = Integer.parseInt(st, base); 
// 把 st 当做base进制 转成10进制的Int 与上面正好相反。
public class Main
{
public static void main( String[] args) {
Scanner cin = new Scanner(System.in);
int num,base;
String st = cin.nextLine();
base = cin.nextInt();
num = Integer.parseInt(st, base);
System.out.println(num);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
//以某个进制读入,然后以某个进制输出。
public class Main
{
public static void main( String[] args) {
Scanner cin = new Scanner(System.in);
BigInteger a;
a = cin.nextBigInteger(2);
String b;
b = a.toString(10);
System.out.println(b);
}
}