有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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。但最终,结果却不一样


共 (2) 个答案

  1. # 1 楼答案

    你的Java代码正确吗?当你的方法返回true时?我觉得你的Java代码很脏。。。来自mbrath的答案是直接转录到Pascal

  2. # 2 楼答案

    类似下面的内容(交错以说明翻译的内容)。到目前为止,您展示的翻译的关键问题是for contador :- 9 downto 1应该是downto 2,以匹配原始代码的功能

    //public static boolean verificaRS(String numeroRS)
    //{
    
    function verificaRS( numeroRS: string ): boolean;
    
    //    Integer numeroAbsoluto = Integer.valueOf(0);
    //    Integer resto = Integer.valueOf(0);
    //    Integer numero = Integer.valueOf(0);
    //    int numeroAuxiliar = 1000000000;
    //    int soma = 0;
    var
        numeroAbsoluto: integer = 0;
        resto: integer = 0;
        numero: integer = 0;
        numeroAuxiliar: integer = 1000000000;
        soma: integer = 0;
    
    //    try
    //    {
    //        numero = Integer.valueOf(Integer.parseInt(numeroRS));
    //    }
    //    catch (Exception ex)
    //    {
    //       Log.escreveLog(ex.toString());
    //       return false;
    //    }
    begin
        try
            numero := StrToInt( numeroRS );
        except
            // This is basic; there is probably a smarter way to handle the exception
            Log.escreveLog( "StrToInt exception" );
            exit( false );
        end;
    
    //    if (numero.intValue() > 0)
    //    {
    
        if numero > 0 then
        begin
    //        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;
    //        }
    
            for contador := 9 downto 2 do
            begin
                numeroAuxiliar := numeroAuxiliar div 10;
                resto := numero mod numeroAuxiliar;
                numeroAbsoluto := numero div numeroAuxiliar;
                numero := numero - numeroAbsoluto * numeroAuxiliar;
                soma := soma + numeroAsboluto * contador;
            end;
    
     //       if (soma % 11 == numero.intValue())
     //           return true;
     //
     //       if ((soma % 11 == 10) && (numero.intValue() == 0))
     //       {
     //           return true;
     //       }
     //
     //       return false;
    
            verificaRS := ((soma mod 11) = numero) or (((soma mod 11) = 10) and (numero = 0));
     //}
    end;