Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static java.lang.Math.*;
- import java.util.Scanner;
- public class nowy {
- //TYLKO MIRKO <3
- public static void main(String[] args) {
- int a, b;
- double e = 0.00001;
- Scanner sc = new Scanner(System.in);
- System.out.println("Podaj poczatek przedzialu");
- a = sc.nextInt();
- System.out.println("Podaj koniec przedzialu");
- b = sc.nextInt();
- System.out.println(polowienie(a,b,e));
- }
- public static double funkcja (double x){
- return 2*x+4;
- }
- public static double polowienie(double a, double b, double e){
- double c;
- if (a>b) {
- System.out.println("Błędnie wprowadzone dane: " + a + ", jest większa od: " + b);
- }
- if (2*a+4 ==0){
- return a;
- }else if (2*b+4 == 0){
- return b;
- }
- while(b-a > e){
- c = (a+b)/2;
- if(funkcja(c) == 0.0) //jesli miejsce zerowe jest w srodku
- return c;
- if(funkcja(a)*funkcja(c)<0)
- b = c;
- else
- a = c;
- }
- return (a+b)/2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment