按照你的要求,C語言程序轉(zhuǎn)換成的Java程序如下:
創(chuàng)新互聯(lián)建站是網(wǎng)站建設(shè)技術(shù)企業(yè),為成都企業(yè)提供專業(yè)的成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì),網(wǎng)站設(shè)計(jì),網(wǎng)站制作,網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制適合企業(yè)的網(wǎng)站。十多年品質(zhì),值得信賴!
public?class?GGG?{
public?static?String?calculateBillMac(String?input_str){
String?str_buff_8?="",str_buff_10?=?"",str_return?=?"";
long?sum8_1=0,sum8_2=0,sum10_1=0,sum10_2=0;//分別存儲(chǔ)八進(jìn)制和十進(jìn)制不同算法取值的合計(jì)
for(int?i=0?;i??input_str.length();i++){//對(duì)輸入字符串的每一個(gè)字節(jié)進(jìn)行循環(huán)?
str_buff_8+=Integer.toOctalString(input_str.charAt(i));//將ASCII碼值轉(zhuǎn)換成八進(jìn)制字符串
str_buff_10+=Integer.toString(input_str.charAt(i));?//將ASCII碼值轉(zhuǎn)換成十進(jìn)制字符串???
}
for(int?i=1;i=str_buff_8.length();i++){??
sum8_1?+=?(str_buff_8.charAt(i?-?1)?-?'0')*i;??
sum8_2?+=?(str_buff_8.charAt(str_buff_8.length()-i)?-?'0')*i;?
}
for(int?i=1;i=str_buff_10.length();i++){??
sum10_1?+=?(str_buff_10.charAt(i?-?1)?-?'0')*i;??
sum10_2?+=?(str_buff_10.charAt(str_buff_10.length()?-?i)?-?'0')*i;?
}
str_return=""+sum8_1%10+sum8_2%10+sum10_1%10+sum10_2%10;
return?str_return;
}
public?static?void?main(String[]?args)?{
System.out.println(calculateBillMac("abcdef"));
}
}
運(yùn)行結(jié)果:
2739
這個(gè)數(shù)據(jù)和C語言用字符串"abcdef"測(cè)試的結(jié)果相同.
package dis;
import java.io.*;
import java.math.*;
public class dis
{
public static void main(String [] s) throws Exception
{
Float a,b,c;
double x2;
double x1;
System.out.println("請(qǐng)輸入a, b, c的值");
BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
a=Float.parseFloat(bReader.readLine());
b=Float.parseFloat(bReader.readLine());
c=Float.parseFloat(bReader.readLine());
if (b*b - 4*a*c = 0)
System.out.println("輸入的系數(shù)不對(duì),b2-4ac不大于0!程序退出!");
else
{
x1 = (-b + Math.sqrt(b*b - 4*a*c))/2*a;
x2 = (-b - Math.sqrt(b*b - 4*a*c))/2*a;
System.out.println("x1 = "+ x1);
System.out.println("x2 = "+x2);
}
return;
}
}
用JBOSS好象可以編譯(記不清了,公司現(xiàn)在不做這個(gè)了,呵呵),其實(shí)C和JAVA差不多的
import?java.util.Scanner;
public?class?code{
public?static?void?main(String?args[]){
int?zhu=0;
int?ji=0;
int?xing?=0;
int?da=0;
System.out.println("請(qǐng)輸入星級(jí):");
Scanner?input?=?new?Scanner(System.in);
xing?=?input.nextInt();
while(xing?!=?0){
System.out.println("請(qǐng)輸入等級(jí):");
ji?=?input.nextInt();
System.out.println("請(qǐng)輸入主屬性:");
zhu?=?input.nextInt();
switch(xing){
case?3:
da=149+zhu-(17+ji)/2;
break;
case?4:
da=149+zhu-(31+ji)/2;
break;
case?5:
da=149+zhu-(49+ji)/2;
break;
}
System.out.println("da="+da);
System.out.printn("請(qǐng)輸入星級(jí):");
xing?=?input.nextInt();
}
}
}
完整的直譯為java方法是這樣的,但是這樣寫dataLen的值必須小于等于數(shù)組長(zhǎng)度,不然會(huì)出現(xiàn)下標(biāo)越界的異常
public?int?accumulativeVerification(int[]?data,?int?dataLen)?{
int?sum?=?0;
for?(int?i?=?0;?i??dataLen;?i++)?{
sum?+=?data[i];
}
return?sum;
}
如果是意譯(計(jì)算int數(shù)組的所有數(shù)之和),那么java可以這樣寫
public?int?accumulativeVerification(int[]?data)?{
int?sum?=?0;
for?(int?i?=?0;?i??data.length;?i++)?{
sum?+=?data[i];
}
return?sum;
}
方法1
PUBLIC short *make_pair_table(const char *structure)
{
/* returns array representation of structure.
table[i] is 0 if unpaired or j if (i.j) pair. */
short i,j,hx;
short length;
short *stack;
short *table;
length = (short) strlen(structure);//structure的長(zhǎng)度
stack = (short *) space(sizeof(short)*(length+1));//stack是一個(gè)short型的指針數(shù)組,這里是分配空間
table = (short *) space(sizeof(short)*(length+2));//table和stack類似
table[0] = length;
for (hx=0, i=1; i=length; i++) {
switch (structure[i-1]) {//訪問structure數(shù)組遍歷,進(jìn)行括號(hào)匹配
case '(':
stack[hx++]=i;//括號(hào)開始就+1
break;
case ')':
j = stack[--hx];
if (hx0) {
fprintf(stderr, "%s\n", structure);
nrerror("unbalanced brackets in make_pair_table");
}
table[i]=j;//存放左括號(hào)所在位置
table[j]=i;//存放右括號(hào)所在位置
break;
default: /* unpaired base, usually '.' */
table[i]= 0;
break;
}
}
if (hx!=0) {
fprintf(stderr, "%s\n", structure);
nrerror("unbalanced brackets in make_pair_table");
}
free(stack);//釋放空間
return(table);//返回括號(hào)匹配結(jié)果
}