BC3-Tolerance is great
public class Main{
public static void main(String[] args){
System.out.println("The size of short is " + Short.SIZE / 8 + " bytes.");
System.out.println("The size of int is " + Integer.SIZE / 8 + " bytes.");
System.out.println("The size of long is " + Long.SIZE / 8 + " bytes.");
System.out.println("The size of long long is " + Long.SIZE / 8 + " bytes.");
}
}
XXX.SIZE: The size of the space occupied by the data type in the memory
BC7-Short Binary
public class Main{
public static void main(String[] args){
int rem[] = new int[10];
String r[] = new String[10];
int num=1234;
int len1=0,len2=0;
int i;
for(i=0;num/8>0;i++){
rem[i]=num%8;
num = num/8;
len1++;
}
rem[len1]=num;
System.out.print("0");
for(i=len1;i>=0;i--){
System.out.print(rem[i]);
}
System.out.print(" ");
num=1234;
for(i=0;num/16>0;i++){
rem[i]=num%16;
num = num/16;
len2++;
}
rem[len2]=num;
for(i=len2;i>=0;i--){
if(rem[i]<10) r[i]=Integer.toString(rem[i]);
else if(rem[i]==10) r[i]="A";
else if(rem[i]==11) r[i]="B";
else if(rem[i]==12) r[i]="C";
else if(rem[i]==13) r[i]="D";
else if(rem[i]==14) r[i]="E";
else r[i]="F";
}
System.out.print("0X");
for(i=len2;i>=0;i--){
System.out.print(r[i]);
}
}
}
Without knowing the API, use the mathematical method to divide 8 (16) and take the remainder, and gain: integer number to string Integer.toString(num) or String.valueOf(num)
Learn other people's code:
Answer 1: Use printf to achieve hexadecimal conversion
public class Main
{
public static void main(String[] args)
{
System.out.printf("0"+"%o",1234);
System.out.printf(" 0X"+"%X",1234);
}
}
Analysis:
- "%d" means output as a decimal integer
- "%o" means output in octal format
- "%x" means output in hexadecimal
- "%X" means output in hexadecimal, and change the letters (A, B, C, D, E, F) to uppercase
- "%e" means output floating-point numbers in scientific notation
- "%E" means output floating-point numbers in scientific notation, and capitalize e
- "%F" means output as a decimal floating point number, adding ".n" between "%f" means keeping n digits after the decimal point when outputting
Answer 2: Use Integer to achieve hexadecimal conversion
public class Main
{
public static void main(String[] args)
{
int i=1234;
System.out.println("0"+Integer.toOctalString(i)+" 0X"+Integer.toHexString(i).toUpperCase());
}
}
Analysis: methods of the Integer class
- Integer.toBinaryString() can convert decimal to binary
- Integer.toOctalString() can convert decimal to octal
- Integer.toHexString() can convert decimal to hexadecimal
- Integer.valueOf(s, radix)
Interpret the first parameter as a signed integer represented by the radix specified by the second parameter
Convert s to decimal in the form of radix - toUpperCase means to convert to uppercase letters
- toLowerCase means to convert to lowercase letters
Answer 3: Use BigInteger to achieve hexadecimal conversion
import java.math.BigInteger;
public class Main
{
public static void main(String[] args)
{
System.out.println("0" + change("1234",10,8) + " 0X" + change("1234",10,16));
}
/**
* number 要转换的数
* from 原数的进制
* to 要转换成的进制
*/
private static String change(String number, int from, int to)
{
String str = new BigInteger(number, from).toString(to);
return str.toUpperCase();
}
}
Analysis:
- change(String number, int from, int to)
- number the number to be converted
- from the base of the original number
- to the base to be converted into
BC8-hexadecimal to decimal
public class Main{
public static void main(String[] args){
String hex = "ABCDEF";
int s = Integer.parseInt(hex,16);
System.out.printf("%15d",s);
}
}
1.integer.parseInt(string s,int radix):
Parameters:
s-String containing the integer representation to be analyzed.
radix-The radix used when analyzing s. If radix is omitted, the radix is 10 (base).
Returns:
the integer represented by the string parameter using the specified radix.
Throws:
NumberFormatException-if the String does not contain a parseable int
2.System.out.printf(); can control the output width and align left and right
System.out.printf("%f",a);
Floating-point number output The
System.out.printf("%+d",b);
output number is signed.
System.out.printf("%-3d",b);
Left-aligned input (right-aligned by default).
Note that if it is,
System.out.printf("%-d",b);
it will prompt an error: java.util.MissingFormatWidthException, left-aligned needs to specify the length, otherwise it is meaningless. The following statement is left-aligned The aligned output also needs to specify the length. Some reference materials are not declared here, which will cause the output to report an error . The output is signed
System.out.printf("%+-6d",b);
and left-justified
System.out.printf("%+9.5d",b);
with a sign, and the symbol occupies a length. 9 represents the length of the output, and 5 represents the value after the decimal point. Number of digits
System.out.printf("%s",s);
output as a string,
System.out.printf("%d,%s,%f",b,s,a);
multiple variables can be output
Return value of BC9-printf
public class Main{
public static void main(String[] args){
System.out.println("Hello world!");
System.out.println("Hello world!".length());
}
}
So the return value of printf is the length of the printed string
BC11-student basic information input and output
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String[] a = s.nextLine().split("[;,]");
int id = Integer.parseInt(a[0]);
double s1 = Double.parseDouble(a[1]);
double s2 = Double.parseDouble(a[2]);
double s3 = Double.parseDouble(a[3]);
System.out.print("The each subject score of No. " + id + " is " + String.format("%.2f", s1) + ", " + String.format("%.2f", s2) + ", " + String.format("%.2f", s3) + ".");
}
}
The split() method splits the string based on matching the given regular expression.
Note: Escape characters such as., $, | and * must be added with \.
Note: For multiple separators, you can use | as a hyphen.
Syntax: public String[] split(String regex, int limit)
Parameters: regex-regular expression separator. limit-the number of copies to split.
Return value: string array.
For example:
System.out.println("");
System.out.println("- 分隔符设置分割份数返回值 :" );
for (String retval: str.split("-", 2)){
System.out.println(retval);
}
Output result:
- 分隔符设置分割份数返回值 :
Welcome
to-Runoob