【问题标题】:My Java program seems to get...stuck (?) at a point during execution. I'm baffled.我的 Java 程序似乎在执行期间的某个时间点卡住了(?)。我很困惑。
【发布时间】: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


【解决方案1】:

检查 while 循环的语法:

 while (more.toUpperCase().charAt(0) == 'Y')

想想程序什么时候会不满足这个表达式。

您的代码中还有多个扫描器。它不清楚为什么您需要一遍又一遍地阅读。

【讨论】:

  • 感谢各位的回复!
  • 这不是答案,而是提示。回答提问者的问题,解释为什么它不起作用——这可能很好,除非我错过了什么。
  • 在这一切中给某人一条鱼。同样非常清楚的是,代码在 main 中读取输入一次,然后在创建 Function 类时再次读取,然后在 Function 类中使用静态方法 x 时再次读取。然后在重复实例化 Function 时再重复 3 次。所以难怪代码一直在寻找用户输入。坦率地说,这段代码有几个错误。我宁愿帮助他们思考他们在做什么。即使这是“严格”的问答网站。为什么不让版主决定我的答案的质量。如果他们愿意,他们可以删除整个线程
猜你喜欢
  • 2014-10-18
  • 2018-05-05
  • 1970-01-01
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 2017-10-20
  • 2023-01-12
  • 2011-04-11
相关资源
最近更新 更多