Java. Where's the extra level from?



  • I don't understand why there's a lot of discharges coming out. at float, there's a nine instead of zero. code:

    class sol_3_2{
        static double f(double x) {
            if (x > 2) return x;
            else return -x;
        }
    
    public static void z(double a, double b, double h) {
        System.out.println("x\t" + "f(x)\t");
        for (double x = a; x <= b; x += h) {
            System.out.println(x + "\t" + f(x) );
        }
    }
    

    }

    public class ex_3_2 {
    public static void main(String[] args) {
    double a = 4;
    double b = 10;
    double h = 0.7;
    sol_3_2.z(a, b, h);
    }
    }

    Conclusion:

    консоль



  • Use the formatted conclusion to 1 decimal place after the decimal point %5.1f:

    System.out.printf("   x\t  f(x)%n");
    for (double x = a; x <= b; x += h) {
        System.out.printf("%5.1f\t%5.1f%n", x, f(x));
    }
    
       x      f(x)
      4.0     4.0
      4.7     4.7
      5.4     5.4
      6.1     6.1
      6.8     6.8
      7.5     7.5
      8.2     8.2
      8.9     8.9
      9.6     9.6
    

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2