Learning the Java language for nearly four weeks, for the Java language, I also have a further understanding, out of the Java language love, I always like to be OK, I knock some very simple code, while learning the Java language, while contrast C language, often can help us to learn more Java words, Since we have learned the C language, every time we write Java programs, we will always be accustomed to the C language grammar to the Java language, but because of the Java and C language syntax and there are some differences, so we write the program can not be compiled. Therefore, through comparative study, we can better understand the Java language, the following problems I encountered and share with you.
I wrote in C and Java, respectively, a program that implements the conversion between two arbitrary binary numbers.
Java Edition:
Package first;
/**
* Digital separation using the conversion of string and integral type
* New
* Date: 2017/9/14 23:37
*/
Import java.util.*;
public class Niubaiquan
{
public static void Main (string[] args) {
TODO auto-generated Method Stub
Char numberarray[]=new char[32];//creates an array to store string numbers
Byte numberarray2[]=new byte[32];//is used to store every number converted
int Sum=0,i,j=0,radi1,radi2;
@SuppressWarnings ("resource")
Scanner input=new Scanner (system.in);
System.out.println ("Please enter a shaping data");
int Number=input.nextint ();
System.out.println ("Please enter this integer in the system");
Radi1=input.nextint ();
System.out.println ("Please enter the number of inputs to be converted into this shaping data");
Radi2=input.nextint ();
if (number<0)
number=-number;//can also use the absolute value method
String data=string.valueof (number);//Convert an integer data to a numeric string
Numberarray=data.tochararray ();//Convert a string into an array of strings
for (i=0;i<numberarray.length;i++)
{
int temp = (numberarray[i]-' 0 ');
Sum+=math.pow (radi1,numberarray.length-1-i) *temp;//converts this number to a decimal number
}
Do
{
Numberarray2[j]= (Byte) (SUM%RADI2);//convert to required binary
Sum/=radi2;
j + +;
}while (sum!=0);
System.out.printf ("%d binary%d converted to%d", radi1,number,radi2);
for (i=j-1;i>=0;i--)
{
System.out.printf ("%d", numberarray2[i]);
}
}
}
Test results
C language version
#include <stdio.h>
#include <math.h>
#define N 50
int main ()
{
int i=0,k=0,j,t,number,number1,number2,radi1,radi2,sum=0;
int a[n],b[n];
printf ("Please enter an integer and its number of digits and the number of entries to be converted: \ n");
scanf ("%d%d%d", &number,&radi1,&radi2);
printf ("%d binary%d converted to%d binary", radi1,number,radi2);
while (number!=0)//separating numbers
{
a[i]=number%10;
NUMBER=NUMBER/10;
i++;
}
for (j=0;j<i;j++)//Convert to Decimal
{
Number1+=a[j]*pow (RADI1,J);
}
do//into the required system
{
B[k]=number1%radi2;
Number1/=radi2;
k++;
}while (number1!=0);
for (t=k-1;t>=0;t--)
{
printf ("%d", b[t]);
}
return 0;
}
Test results
In the two programs I feel the biggest difference is the Java language and the C language for the processing of strings is very different, in C, string and string array is not much difference, basically through a string array to do the operation of the string, and in the Java language, the string has a special processing method, Although the two can be transformed by the string.valueof () and ToCharArray () methods, they go in different two data types.
201671010127 2016-2017-2 The re-understanding of Java through a small program.