gpt4 book ai didi

java - 尝试找到带有循环的数学函数之和的分辨率(java)

转载 作者:行者123 更新时间:2023-12-02 11:39:51 25 4
gpt4 key购买 nike

我正在尝试寻找这个函数的答案。我必须从用户处获取 0,8 作为实数(双 x),结果必须是 0.71736,但我的程序不会计算它。

当总和达到10^-6时程序必须停止。这是函数: (-1 ^ n) * [x ^(2*n +1)/(2*n +1)!]我的错误是什么?我看不到。

import java.util.Scanner;

public class sumFunction {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter a reel number ");
double x = keyboard.nextDouble();

double sum = 0;
int n = 0;

while (true) {


double firstPart = Math.pow(-1, n); // first part of function
double numerator = Math.pow(x , 2 * n + 1); // top of the function
double denominator = 1; // bottom of the function


for (int i = 2 * n + 1; i >= 1; i--) {


denominator = denominator * i;


}


double function = firstPart * (numerator / denominator);

n = n + 1;
sum += function;

if (sum <= Math.pow(10 , -6)) {

System.out.println("Sum:" + sum);

}

}

}

}

最佳答案

如果你想停止循环,你应该结束循环。与 break;

if (sum <= Math.pow(10 , -6)) {

System.out.println("Sum:" + sum);
break; // this end while loop
}

或者使用

java.lang.System.exit(0);

终止程序

查看这段代码

import java.util.Scanner;

public class sumFunction {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter a reel number ");
double x = keyboard.nextDouble();

double sum = 0;
int n = 0;

while (true) {


double firstPart = Math.pow(-1, n); // first part of function
double numerator = Math.pow(x , 2 * n + 1); // top of the function
double denominator = 1; // bottom of the function


for (int i = 2 * n + 1; i >= 1; i--) {
denominator = denominator * i;
}


double function = firstPart * (numerator / denominator);

System.out.println(" " + function + " = " +firstPart + " * "+ "(" +numerator+ " / "+ denominator + ")" ); //Add for debug
System.out.println("Sum:" + sum);//Add for debug
n = n + 1;
sum += function;

if (sum <= Math.pow(10 , -6)) {

System.out.println("Sum:" + sum);

}

}

}}

输出:

 0.8 = 1.0 * (0.8 / 1.0)
Sum:0.0
-0.08533333333333336 = -1.0 * (0.5120000000000001 / 6.0)
Sum:0.8
0.0027306666666666673 = 1.0 * (0.3276800000000001 / 120.0)
Sum:0.7146666666666667
-4.161015873015875E-5 = -1.0 * (0.20971520000000007 / 5040.0)
Sum:0.7173973333333333
3.698680776014111E-7 = 1.0 * (0.13421772800000006 / 362880.0)
Sum:0.7173557231746032
-2.151959724226392E-9 = -1.0 * (0.08589934592000005 / 3.99168E7)
Sum:0.7173560930426808
8.828552714774943E-12 = 1.0 * (0.054975581388800036 / 6.2270208E9)
Sum:0.717356090890721
-2.6906065416456974E-14 = -1.0 * (0.03518437208883203 / 1.307674368E12)
Sum:0.7173560908995497
6.330838921519288E-17 = 1.0 * (0.022517998136852502 / 3.55687428096E14)
Sum:0.7173560908995228
-1.1847183946702764E-19 = -1.0 * (0.014411518807585602 / 1.21645100408832E17)
Sum:0.7173560908995229
1.8052851728308975E-22 = 1.0 * (0.009223372036854787 / 5.109094217170944E19)
Sum:0.7173560908995229
-2.283364645477815E-25 = -1.0 * (0.005902958103587064 / 2.585201673888498E22)
Sum:0.7173560908995229
2.435588955176337E-28 = 1.0 * (0.0037778931862957215 / 1.5511210043330984E25)
Sum:0.7173560908995229
-2.220479959135122E-31 = -1.0 * (0.002417851639229262 / 1.0888869450418352E28)
Sum:0.7173560908995229
1.750131987495663E-34 = 1.0 * (0.0015474250491067279 / 8.841761993739701E30)
Sum:0.7173560908995229
-1.2043919053733592E-37 = -1.0 * (9.903520314283058E-4 / 8.222838654177924E33)
Sum:0.7173560908995229
7.299344881050661E-41 = 1.0 * (6.338253001141158E-4 / 8.68331761881189E36)
Sum:0.7173560908995229
-3.9256980872877526E-44 = -1.0 * (4.0564819207303417E-4 / 1.0333147966386144E40)
Sum:0.7173560908995229
1.886221303201323E-47 = 1.0 * (2.5961484292674194E-4 / 1.3763753091226346E43)
Sum:0.7173560908995229
-8.145625061058344E-51 = -1.0 * (1.6615349947311485E-4 / 2.0397882081197447E46)
Sum:0.7173560908995229
3.1787805116325264E-54 = 1.0 * (1.0633823966279351E-4 / 3.34525266131638E49)
Sum:0.7173560908995229
-1.1264781436571518E-57 = -1.0 * (6.805647338418786E-5 / 6.041526306337384E52)
Sum:0.7173560908995229
3.64114147444736E-61 = 1.0 * (4.355614296588023E-5 / 1.196222208654802E56)
Sum:0.7173560908995229
-1.0778587158401064E-64 = -1.0 * (2.7875931498163353E-5 / 2.5862324151116827E59)
Sum:0.7173560908995229
2.932948886639745E-68 = 1.0 * (1.7840596158824548E-5 / 6.082818640342679E62)
Sum:0.7173560908995229
-7.36112661744878E-72 = -1.0 * (1.1417981541647711E-5 / 1.5511187532873816E66)
Sum:0.7173560908995229
1.7094053102928952E-75 = 1.0 * (7.307508186654536E-6 / 4.274883284060024E69)
Sum:0.7173560908995229
-3.6835669986109553E-79 = -1.0 * (4.676805239458904E-6 / 1.2696403353658264E73)
Sum:0.7173560908995229
7.385597992202408E-83 = 1.0 * (2.9931553532536988E-6 / 4.052691950487723E76)
Sum:0.7173560908995229
-1.3812924357129002E-86 = -1.0 * (1.9156194260823675E-6 / 1.386831185456898E80)
Sum:0.7173560908995229
2.4153747509733775E-90 = 1.0 * (1.2259964326927152E-6 / 5.075802138772246E83)
Sum:0.7173560908995229
-3.957603278604612E-94 = -1.0 * (7.846377169233378E-7 / 1.9826083154044396E87)
Sum:0.7173560908995229
6.08862042862248E-98 = 1.0 * (5.021681388309362E-7 / 8.247650592082472E90)
Sum:0.7173560908995229
-8.812114595925793E-102 = -1.0 * (3.2138760885179925E-7 / 3.6471110918188705E94)
Sum:0.7173560908995229
1.2019934657699302E-105 = 1.0 * (2.0568806966515154E-7 / 1.7112245242814127E98)
Sum:0.7173560908995229
-1.5478386681946784E-109 = -1.0 * (1.31640364585697E-7 / 8.504785885678624E101)
Sum:0.7173560908995229
1.8847350602066097E-113 = 1.0 * (8.424983333484609E-8 / 4.470115461512686E105)
Sum:0.7173560908995229
-2.1733881775355505E-117 = -1.0 * (5.39198933343015E-8 / 2.4809140811395404E109)
Sum:0.7173560908995229
2.3769111989452365E-121 = 1.0 * (3.450873173395297E-8 / 1.451830920282859E113)
Sum:0.7173560908995229
-2.468716597411475E-125 = -1.0 * (2.20855883097299E-8 / 8.94618213078298E116)
Sum:0.7173560908995229
2.4382386147273843E-129 = 1.0 * (1.4134776518227139E-8 / 5.797126020747369E120)
Sum:0.7173560908995229
-2.2927897640692413E-133 = -1.0 * (9.04625697166537E-9 / 3.94552396972066E124)
Sum:0.7173560908995229
2.0551616932833552E-137 = 1.0 * (5.789604461865837E-9 / 2.8171041143805494E128)
Sum:0.7173560908995229
-1.7579570752490613E-141 = -1.0 * (3.705346855594136E-9 / 2.107757298379527E132)
Sum:0.7173560908995229
1.4365328500503047E-145 = 1.0 * (2.3714219875802474E-9 / 1.6507955160908465E136)
Sum:0.7173560908995229
-1.1225653529086632E-149 = -1.0 * (1.5177100720513585E-9 / 1.3520015276784033E140)
Sum:0.7173560908995229
8.396935786133066E-154 = 1.0 * (9.713344461128696E-10 / 1.156772507081641E144)
Sum:0.7173560908995229
-6.0179606977885315E-158 = -1.0 * (6.216540455122366E-10 / 1.0329978488239061E148)
Sum:0.7173560908995229
4.136055462397616E-162 = 1.0 * (3.9785858912783145E-10 / 9.619275968248216E151)
Sum:0.7173560908995229
-2.728381257405149E-166 = -1.0 * (2.5462949704181216E-10 / 9.332621544394415E155)
Sum:0.7173560908995229
1.728875252217124E-170 = 1.0 * (1.629628781067598E-10 / 9.42594775983836E159)
Sum:0.7173560908995229
-1.0531888077469635E-174 = -1.0 * (1.0429624198832628E-10 / 9.902900716486178E163)
Sum:0.7173560908995229
6.172535136978538E-179 = 1.0 * (6.674959487252883E-11 / 1.0813967582402912E168)
Sum:0.7173560908995229
-3.4830034276726043E-183 = -1.0 * (4.2719740718418454E-11 / 1.2265202031961373E172)
Sum:0.7173560908995229
1.8935798451499023E-187 = 1.0 * (2.7340634059787814E-11 / 1.4438595832024942E176)
Sum:0.7173560908995229
-9.925398041735769E-192 = -1.0 * (1.7498005798264204E-11 / 1.7629525510902457E180)
Sum:0.7173560908995229
5.019164622875235E-196 = 1.0 * (1.1198723710889092E-11 / 2.2311927486598138E184)
Sum:0.7173560908995229
-2.4502405481618247E-200 = -1.0 * (7.167183174969019E-12 / 2.925093693493014E188)
Sum:0.7173560908995229
1.1554332086822632E-204 = 1.0 * (4.586997231980173E-12 / 3.96993716080872E192)
Sum:0.7173560908995229
-5.266181837036379E-209 = -1.0 * (2.935678228467311E-12 / 5.574585761207606E196)
Sum:0.7173560908995229
2.3211820769306353E-213 = 1.0 * (1.878834066219079E-12 / 8.094298525273444E200)
Sum:0.7173560908995229
-9.89975029478613E-218 = -1.0 * (1.2024538023802108E-12 / 1.2146304367025332E205)
Sum:0.7173560908995229
4.0876388313955647E-222 = 1.0 * (7.69570433523335E-13 / 1.882677176888926E209)
Sum:0.7173560908995229
-1.634851176161204E-226 = -1.0 * (4.925250774549344E-13 / 3.0126600184576624E213)
Sum:0.7173560908995229
6.336632465741109E-231 = 1.0 * (3.152160495711581E-13 / 4.974504222477287E217)
Sum:0.7173560908995229
-2.3813533635198552E-235 = -1.0 * (2.017382717255412E-13 / 8.471580690878813E221)
Sum:0.7173560908995229
8.681169700687555E-240 = 1.0 * (1.2911249390434637E-13 / 1.4872707060906847E226)
Sum:0.7173560908995229
-3.071281707263701E-244 = -1.0 * (8.26319960987817E-14 / 2.690472707318049E230)
Sum:0.7173560908995229
1.0549701012498761E-248 = 1.0 * (5.288447750322029E-14 / 5.0128887482749884E234)
Sum:0.7173560908995229
-3.519866879365657E-253 = -1.0 * (3.384606560206099E-14 / 9.615723196941089E238)
Sum:0.7173560908995229
1.1411929092168288E-257 = 1.0 * (2.1661481985319034E-14 / 1.8981437590761713E243)
Sum:0.7173560908995229
-3.5967864764048624E-262 = -1.0 * (1.3863348470604184E-14 / 3.8543707171800694E247)
Sum:0.7173560908995229
1.1024632877869307E-266 = 1.0 * (8.87254302118668E-15 / 8.047926057471989E251)
Sum:0.7173560908995229
-3.287561756516797E-271 = -1.0 * (5.678427533559475E-15 / 1.72724589045464E256)
Sum:0.7173560908995229
9.541263940553021E-276 = 1.0 * (3.634193621478064E-15 / 3.8089226376305687E260)
Sum:0.7173560908995229
-2.695986279008358E-280 = -1.0 * (2.3258839177459616E-15 / 8.627209774233244E264)
Sum:0.7173560908995229
7.419294885471915E-285 = 1.0 * (1.4885657073574156E-15 / 2.0063439050956838E269)
Sum:0.7173560908995229
-1.9892537606627693E-289 = -1.0 * (9.526820527087462E-16 / 4.789142901463393E273)
Sum:0.7173560908995229
5.198115330818934E-294 = 1.0 * (6.097165137335975E-16 / 1.1729568794264134E278)
Sum:0.7173560908995229
-1.3242551595112325E-298 = -1.0 * (3.902185687895025E-16 / 2.946702272495036E282)
Sum:0.7173560908995229
3.290074930462685E-303 = 1.0 * (2.497398840252816E-16 / 7.590705053947223E286)
Sum:0.7173560908995229
-7.974126923790499E-308 = -1.0 * (1.5983352577618024E-16 / 2.0044015765453032E291)
Sum:0.7173560908995229
1.88597236926E-312 = 1.0 * (1.0229345649675537E-16 / 5.423910666131583E295)
Sum:0.7173560908995229
-4.354023E-317 = -1.0 * (6.546781215792344E-17 / 1.5036165148649983E300)
Sum:0.7173560908995229
9.83E-322 = 1.0 * (4.189939978107101E-17 / 4.2690680090047056E304)
Sum:0.7173560908995229
-0.0 = -1.0 * (2.681561585988545E-17 / Infinity)
Sum:0.7173560908995229

你应该通过分子之类的东西检查循环的结束这是你想要的;)

import java.util.Scanner;

public class sumFunction {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter a reel number ");
double x = keyboard.nextDouble();

double sum = 0;
int n = 0;

while (true) {


double firstPart = Math.pow(-1, n); // first part of function
double numerator = Math.pow(x , 2 * n + 1); // top of the function
double denominator = 1; // bottom of the function


for (int i = 2 * n + 1; i >= 1; i--) {
denominator = denominator * i;
}


double function = firstPart * (numerator / denominator);

// System.out.println(" " + function + " = " +firstPart + " * "+ "(" +numerator+ " / "+ denominator + ")" ); //Add for debug
// System.out.println("Sum:" + sum);//Add for debug
n = n + 1;
sum += function;

if (numerator <= Math.pow(10 , -6)) {

System.out.println("Sum:" + sum);
break;
}

}

}}

关于java - 尝试找到带有循环的数学函数之和的分辨率(java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48666181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com