【发布时间】:2014-10-13 21:56:10
【问题描述】:
所以,我正在尝试为我的方法创建一个驱动程序。我提前道歉,我对我在说什么知之甚少。该程序所做的是,它通过泰勒级数为用户输入的数字计算正弦、余弦和指数函数。不允许使用 Math.pow 和 Math.fact。我的编译器没有给我任何错误,我现在完全没有想法。此外,在我按下回车后,扫描仪似乎并没有停止接受输入。它继续接受数字,但不做任何其他事情。当我输入一个字母时,它给出了一个例外。 ID10T 错误的可能性很大。我知道输出的格式还不是很好,但那是因为我还没有机会看到它。如果有人可以帮助我解决这个问题,我将非常感激。提前致谢!
-一个有抱负的代码猴子
驱动程序(Lab4.java)
/*
This program is being created to solve Lab 4.
*/
import java.util.*;
public class Lab4
{
public static void main(String[] args)
{
String again, more = "y";
while (more.toUpperCase().charAt(0) == 'Y')
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Input X: ");
Function number = new Function();
double x = number.x();
System.out.println("x = " + x);
Function sine = new Function();
double mySin = sine.funcSine();
Function cosine = new Function();
double myCos = cosine.funcCos();
Function expo = new Function();
double myExp = expo.funcExp();
System.out.println("\t\t\t\tLibraryResult My Result");
System.out.println("\n\t\tsin(" + Function.x() + ")" + " = " + "\t\t\t" + Math.sin(Function.x()) + "\t" + mySin);
System.out.println("\n\t\tcos(" + Function.x() + ")" + " = " + "\t\t\t" + Math.cos(Function.x()) + "\t" + myCos);
System.out.println("\n\t\texp(" + Function.x() + ")" + " = " + "\t\t\t" + Math.exp(Function.x()) + "\t" + myExp);
System.out.println("\n\t\t\tDo more (Y/N) ? ");
more = keyboard.next();
String junk = keyboard.nextLine();
}
}
}
方法(Function.java)
/*
This class provides the information for the parent to use in order to solve Lab 4.
*/
import java.util.*;
public class Function
{
Scanner keyboard = new Scanner(System.in);
public static int number;
private double temp = 1;
private double last = 1;
public static double x()
{
Scanner keyboard = new Scanner(System.in);
double x = keyboard.nextDouble();
return x;
}
public static double pow(double value, double power)
{
if(power == 0)
{
return 1;
}
double result = value;
for(int i = 0; i < power -1; i++)
{
result = result * value;
}
return result;
}
public static double getFact()
{
while (number < 0)
{
number =(number * -1);
}
double factorial = 1;
if (0 == (number % 1))
{
do
{
factorial = factorial * number;
--number;
} while (number >= 1);
}
else //Stirling's Approximation
{
double e = 2.718281828459;
double part1 = Math.sqrt(2 * 3.141592653589 * number);
double part2a = (number / e);
double part2b = (pow(part2a,number));
factorial = (part1 * part2b);
}
return factorial;
}
public static double funcSine()
{
int place = 0;
double next = x()*1;
for(number = 3; number <= 30; number = number + 2)
{
next = next + ((pow((-1),place))*((pow(x(),number))/(getFact())));
place = place + 1;
}
double mySin = next * 1;
return mySin;
}
public static double funcCos()
{
int place = 0;
double next = 1;
for(number = 2; number <= 30; number = number + 2)
{
next = next + ((pow(-1,place))*((pow(x(),number))/(getFact())));
place = place + 1;
}
double myCos = next * 1;
return myCos;
}
public static double funcExp()
{
int place = 0;
double next = 1 + x();
for(number = 1; number <= 30; number++)
{
next = next + ((pow(-1,place))*((pow(x(),number))/(getFact())));
place = place + 1;
}
double myExp = next * 1;
return myExp;
}
}
【问题讨论】:
-
这听起来像是学习使用调试器的绝佳机会。从长远来看,这将为您节省无数小时。
-
我尝试调试,但没有快速完成。没有多少 google-fu 帮助我解决了我收到的信息。此后不久,调试器决定完全忽略我的断点。它只是立即跳到最后。我正要为此拔头发。我只是不明白为什么它不想继续进入 X。
-
随机提示:在折腾输入时,不需要将其保存到变量中。只需调用该方法而不将其分配给任何东西,它就会做同样的事情,而没有内存分配/释放开销。
标签: java methods driver java.util.scanner