Java到Pascal示例
我是编程新手,一点也不懂Java。下面是我无法用Pascal理解的Java代码:
public static boolean verificaRS(String numeroRS)
{
Integer numeroAbsoluto = Integer.valueOf(0);
Integer resto = Integer.valueOf(0);
Integer numero = Integer.valueOf(0);
int numeroAuxiliar = 1000000000;
int soma = 0;
try
{
numero = Integer.valueOf(Integer.parseInt(numeroRS));
}
catch (Exception ex)
{
Log.escreveLog(ex.toString());
return false;
}
if (numero.intValue() > 0)
{
for (int contador = 9; contador > 1; contador--)
{
numeroAuxiliar /= 10;
resto = Integer.valueOf(numero.intValue() % numeroAuxiliar);
numeroAbsoluto = Integer.valueOf(numero.intValue() / numeroAuxiliar);
numero = Integer.valueOf(numero.intValue() - numeroAbsoluto.intValue() * numeroAuxiliar);
soma += numeroAbsoluto.intValue() * contador;
}
if (soma % 11 == numero.intValue())
return true;
if ((soma % 11 == 10) && (numero.intValue() == 0))
{
return true;
}
return false;
}
return false;
}
有人能帮我翻译吗?到目前为止,我就是这么做的:
NumeroAbsoluto, Resto, Numero, NumeroAuxiliar, Soma: Integer;
Contador: Integer;
begin
numeroAuxiliar := 1000000000;
Numero := 00009356332;
for Contador := 9 downto 1 do
begin
NumeroAuxiliar := NumeroAuxiliar div 10;
Resto := Numero mod NumeroAuxiliar;
NumeroAbsoluto := Numero div NumeroAuxiliar;
Numero := Numero - NumeroAbsoluto * NumeroAuxiliar;
Soma := Soma + NumeroAbsoluto * Contador;
end;
本案数字为00009356332。但最终,结果却不一样
# 1 楼答案
你的Java代码正确吗?当你的方法返回true时?我觉得你的Java代码很脏。。。来自mbrath的答案是直接转录到Pascal
# 2 楼答案
类似下面的内容(交错以说明翻译的内容)。到目前为止,您展示的翻译的关键问题是
for contador :- 9 downto 1
应该是downto 2
,以匹配原始代码的功能