Thread: PHP to Java

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    PHP to Java


    Hello, I am trying to convert the following php code to java:

    PHP Code:
    <?php

    $e_tax 
    70;
    $e_peasants 0;
    $e_freeland 0
    $e_land 10000;
    $e_bldpop 10000;

    for(
    $i 1$i <= 10000$i++){
      
    $taxrate $e_tax 100;
      if (
    $taxrate 0.40)
       
    $taxpenalty = ($taxrate 0.40) / 2;
      elseif (
    $taxrate 0.20)
        
    $taxpenalty = ($taxrate 0.20) / 2;
      else    
    $taxpenalty 0;

      
    $popbase round((($e_land 2) + ($e_freeland 5) + ($e_bldpop 60)) / (0.95 $taxrate $taxpenalty));

      
    $peasants 0;
      
    $peasmult 1;
      if (
    $e_peasants != $popbase)
          
    $peasants = ($popbase $e_peasants) / 20;
      if (
    $peasants 0)
        
    $peasmult = (/ (($e_tax 15) / 20)) - (9);
      if (
    $peasants 0)
        
    $peasmult / ((/ (($e_tax 15) / 20)) - (9));

      
    $peasants round($peasants $peasmult $peasmult);
      
    $e_peasants += $peasants;
      print 
    "$e_peasants<br>";
    }

    ?>
    I converted it to the following:
    Code:
    package game;
    
    public class Main4 {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            long e_tax = 70;
            long e_peasants = 0;
            long e_freeland = 0;
            long e_land = 10000;
            long e_bldpop = 10000;
            for (int i = 1;i<=10000;i++) {
                double taxrate = (e_tax / 100);
                double taxpenalty;
                if (taxrate > 0.40){
                    taxpenalty = (taxrate - 0.40) / 2;
                }
                else if (taxrate < 0.20){ 
                    taxpenalty = (taxrate - 0.20) / 2;
                }
                else {taxpenalty = 0;}
    
                long popbase = (int) Math.round(((e_land * 2)
            + (e_freeland * 5) + (e_bldpop * 60))
            / (0.95 + taxrate + taxpenalty));
    
                long peasants = 1;
                double peasmult = 1;
                if (e_peasants != popbase) {
                    peasants = (popbase - e_peasants) / 20;
                }
                if (peasants > 0) {
                    double a = ((e_tax + 15) / 20);
                    peasmult = (4 / a) - (7 / 9);
                }
                if (peasants < 0) {
                    double a = ((e_tax + 15) / 20);
                    peasmult = 1 / ((4 / a) - (7 / 9));
                }
                peasants = Math.round(peasants * peasmult * peasmult);
                e_peasants = e_peasants + peasants;
                System.out.println(e_peasants);
            }
        }
    
    }
    But the output is different, can someone help me out with this, so that the java will be the same output as the php(without modifying the php)?
    Why is it that I got different outputs of both?
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you post the output from both programs to show what is different?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    Ah I got the problem, it is a floating point so I have to put instead of tax/100, tax/100.0

IMN logo majestic logo threadwatch logo seochat tools logo