谁可以把这段PASCAL程序译成JAVA语句? ( 积分: 200 )

  • 主题发起人 主题发起人 飘叶
  • 开始时间 开始时间

飘叶

Unregistered / Unconfirmed
GUEST, unregistred user!
2 :=1 ;
n1:=8892636;
for i:=1 to length(s1)do

n2:=abs((ord(s1)*n2 + $f48)) mod 1000000;
for i:=2 to length(s2)do
n2:=abs((ord(s2)*n2 +$F83)) mod 1000000;
n2:=10705672 +abs((n2+n1) *(length(s1)+length(s2)));
S5:= zjs+copy(inttostr(n2),1,6);
n2:=strtoint(zjs+copy(inttostr(n2),1,6));
n2:=137964276 +abs((n2 div 181)*196 xor 39694322);
s1:=copy(inttostr(n2),1,8);
s2:='14723946';
s4:='';
for i:=1 to 8do

s4:=s4 +s1[ord(s2)- ord('1')+1];
s6:=s4+'-'+S5;
 
2 :=1 ;
n1:=8892636;
for i:=1 to length(s1)do

n2:=abs((ord(s1)*n2 + $f48)) mod 1000000;
for i:=2 to length(s2)do
n2:=abs((ord(s2)*n2 +$F83)) mod 1000000;
n2:=10705672 +abs((n2+n1) *(length(s1)+length(s2)));
S5:= zjs+copy(inttostr(n2),1,6);
n2:=strtoint(zjs+copy(inttostr(n2),1,6));
n2:=137964276 +abs((n2 div 181)*196 xor 39694322);
s1:=copy(inttostr(n2),1,8);
s2:='14723946';
s4:='';
for i:=1 to 8do

s4:=s4 +s1[ord(s2)- ord('1')+1];
s6:=s4+'-'+S5;
 
主要目的是干什么?
 
//简单的翻译了一下,有几处没看明白
1、2 :=1 ;
--> n2 := 1;

2、怎么都是n2?
for i:=1 to length(s1)do

n2:=abs((ord(s1)*n2 + $f48)) mod 1000000;
for i:=2 to length(s2)do
n2:=abs((ord(s2)*n2 +$F83)) mod 1000000;
3、变量太多,而且没注释
//
public class Test{
public static void main(String[] args){
int n2 = 1 ;
int n1 = 8892636;
String s1 = new String("");
String s2 = new String("14723946");
String s4 = new String("");
String s5 = new String("");
String s6 = new String("");
String zjs = new String("");
for (int i=1;i<=s1.length();i++)
n2 = Math.abs(((int)(s1.charAt(i)) * n2 + 0xf48)) % 1000000;
for (int i=2;
i<=s2.length();
i++)
n2 = Math.abs(((int)(s2.charAt(i))* n2 + 0x0F83)) % 1000000;
n2 = 10705672 + Math.abs((n2+n1) *(s1.length()+s2.length()));
//s5 = n2.toString();
s5 = zjs + String.format(&quot;%d&quot;, n2).substring(1, 6);
n2 = Integer.parseInt(s5);
n2 = 137964276 + Math.abs((n2 / 181) * 196 ^ 39694322);
s1 = String.format(&quot;%d&quot;, n2).substring(1, 8);
for (int i=1;
i<=8;
i++)
s4 = s4 + s1.charAt((int)(s2.charAt(i))- (int)('1') + 1);
s6 = s4 + '-' + s5;
}
}
 
对不起,我贴得太匆忙了
开头的 2 :=1 ;
应为 n2 := 1;
在你的基础上我改了一下:
==========================
int n2 = 1 ;
int n1 = 8892636;
String s1 = new String(&quot;&quot;);
String s2 = new String(&quot;&quot;);
String s4 = new String(&quot;&quot;);
String s5 = new String(&quot;&quot;);
String s6 = new String(&quot;&quot;);
String zjs = new String(&quot;&quot;);
for (int i=1;i<=s1.length();i++)
n2 = Math.abs(((int)(s1.charAt(i)) * n2 + 0xf48)) % 1000000;
for (int i=2;
i<=s2.length();
i++)
n2 = Math.abs(((int)(s2.charAt(i))* n2 + 0x0F83)) % 1000000;
n2 = 10705672 + Math.abs((n2+n1) *(s1.length()+s2.length()));
//s5 = n2.toString();
s5 = zjs + String.format(&quot;%d&quot;, n2).substring(1, 6);
n2 = Integer.parseInt(s5);
n2 = 137964276 + Math.abs((n2 / 181) * 196 ^ 39694322);
s1 = String.format(&quot;%d&quot;, n2).substring(1, 8);
s2 = &quot;14723946&quot;;
/* 应在这赋值*/
for (int i=1;
i<=8;
i++)
s4 = s4 + s1.charAt((int)(s2.charAt(i))- (int)('1') + 1);
s6 = s4 + '-' + s5;
================================
但编译时 String.format出错!
 
你的JRE是哪个版本,我编译正常,但运行出错
( java -version
java version &quot;1.5.0_04&quot;
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)
 
我的DELPHI源程序是
procedure TForm1.Button1Click(Sender: TObject);
var
s1,s2,s4,s5,s6,zjs:string;
i,n1,n2:integer;
begin
s1:='ABCD00500100291C';
s2:='QWERTYUIO7';
zjs:='052';
n2 :=1 ;
n1:=8892636;
for i:=1 to length(s1)do
n2:=abs((ord(s1)*n2 + $f48)) mod 1000000;
for i:=2 to length(s2)do
n2:=abs((ord(s2)*n2 +$F83)) mod 1000000;
n2:=10705672 +abs((n2+n1) *(length(s1)+length(s2)));
S5:= zjs+copy(inttostr(n2),1,6);
n2:=strtoint(zjs+copy(inttostr(n2),1,6));
n2:=137964276 +abs((n2 div 181)*196 xor 39694322);
s1:=copy(inttostr(n2),1,8);
s2:='73461852';
s4:='';
for i:=1 to 8do
s4:=s4 +s1[ord(s2)- ord('1')+1];
s6:=s4+'-'+S5;

edit1.Text:=s6;
end;
运行后结果为:74931425-052253958
======================
我用的是eclipse 3。0。1版,我把程序又改了一下,能运行了,但结果与DELPHI不同
public void commandAction(Command arg0, Displayable arg1)
{
String s1 = &quot;ABCD00500100291C&quot;;
String zjs = &quot;052&quot;;
String s2 = &quot;QWERTYUIO7&quot;;
int n2 = 1 ;
int n1 = 8892636;
String s4 = new String(&quot;&quot;);
String s5 = new String(&quot;&quot;);
String s6 = new String(&quot;&quot;);
for (int i=1;i<s1.length();i++)
//发现这里i<=s1.length()会进入死循环,所以下面的都去掉=号
n2 = Math.abs(((int)(s1.charAt(i)) * n2 + 0xf48)) % 1000000;
//在这里计算出的 n2 就与 DELPHI的不同了
for (int i=2;
i<s2.length();
i++)
n2 = Math.abs(((int)(s2.charAt(i))* n2 + 0x0F83)) % 1000000;

n2 = 137964276 + Math.abs((n2+n1) *(s1.length()+s2.length()));
s5 = zjs + String.valueOf(n2).substring(1, 6);

n2 = 137964276 + Math.abs((n2 / 181) * 196 ^ 39694322);
s1 = String.valueOf(n2).substring(1, 8);
s2 =&quot;73461852&quot;;
for (int i=1;
i<8;
i++)
s4 = s4 + s1.charAt((int)(s2.charAt(i))- (int)('1') + 1);

s6 = s4 + '-' + s5;
}
 
public class Test{
public static void main(String[] args){
long n2 = 1 ;
long n1 = 8892636;
String s1 = new String(&quot;&quot;);
String s2 = new String(&quot;&quot;);
String s4 = new String(&quot;&quot;);
String s5 = new String(&quot;&quot;);
String s6 = new String(&quot;&quot;);
String zjs = new String(&quot;&quot;);
s1 = &quot;ABCD00500100291C&quot;;
s2 = &quot;QWERTYUIO7&quot;;
zjs = &quot;052&quot;;
for (int i=1;i<=s1.length();i++)
n2 = Math.abs(((int)(s1.charAt(i-1)) * n2 + 0xf48)) % 1000000;
for (int i=2;
i<=s2.length();
i++)
n2 = Math.abs(((int)(s2.charAt(i-1))* n2 + 0x0F83)) % 1000000;
n2 = 10705672 + Math.abs((n2+n1) *(s1.length()+s2.length()));
s5 = zjs + String.format(&quot;%d&quot;, n2).substring(0, 6);
n2 = Integer.parseInt(s5);
n2 = 137964276 + Math.abs(((n2 / 181) * 196) ^ 39694322);
s1 = String.format(&quot;%d&quot;, n2).substring(0, 8);
s2 = &quot;73461852&quot;;
for (int i=1;
i<=8;
i++)
s4 = s4 + s1.charAt((int)(s2.charAt(i-1))- (int)('1'));
s6 = s4 + '-' + s5;
System.out.println(s6);
}
}
 
哈哈,xianguo果然是高手,终于解决了!给分!
不过有一个问题,当s1含有汉字时,结果就不同了,因为
如 S1='汉字' 在DELPHI中 length(s1) = 4,而在JAVA中 s1.length() = 2
我不知如何设置,才能使s1.length() = 4
 
后退
顶部