-1

当我用 issue1 对象调用方法“getUnknownsAccel”时,由于某种原因,该方法中的“if”语句未执行以检索变量的值:

PhysicsProblem problem1 = new PhysicsProblem(accel, vI, vF, t, deltaX);

    System.out.println("Which variable are you solving for? ");
    String solveFor = scan.next();


    // after receiving solveFor input, assesses data accordingly

    if (solveFor.equalsIgnoreCase("acceleration"))
    {
        System.out.println("Solving for Acceleration!");
        System.out.println("Are there any other unknowns? (enter 'none' or the name " +
                "of the variable)");
        missingVar = scan.next();
        problem1.setMissingVar(missingVar);
        do
        {
            problem1.getUnknownsAccel();
            System.out.println("Are there any other unknowns? (enter 'none' or the name " +
                    "of the variable)");
            missingVar = scan.next();               //// change all these in the program to scan.next, not scan.nextLine
        }
        while (!missingVar.equalsIgnoreCase("none") || !missingVar.equalsIgnoreCase("acceleration"));

        if (missingVar.equals("none"))
        {
            // Write code for finding solutions
            System.out.println("Assuming you have given correct values, the solution is: ");
        }
    }

在用于检索其他未知变量名称的 do/while 循环之后,我从这个类文件中调用 getUnknownsAccel 方法:

public void getUnknownsAccel()
{
    //-----------
    // checks for another unknown value that is not accel
    //-----------
    if (missingVar.equalsIgnoreCase("time"))
    {
        System.out.println("Please enter the value for time: ");
        t = scan.nextDouble();
        while (t <= 0 || !scan.hasNextDouble())
        {
            System.out.println("That is not an acceptable value!");
            t = scan.nextDouble();
        }
    }       

}

让我们假设为了这个问题,用户会在提示时输入“时间”作为未知数。知道为什么我的代码没有执行扫描函数来检索时间变量值吗?相反,程序只是重复 system.out 函数“是否还有其他未知数......”

4

1 回答 1

2

扫描后,您将missingVar 设置为scan.next(),但您不执行任何操作。循环继续。

missingVar = scan.next();

添加行

getUnknownsAccel();

请注意,另一个问题是您稍后需要处理的是 missingVar 是本地的 - 要在 getUnknownsAccel() 中访问它,您应该将声明更改为

public void getUnknownsAccel(String missingVar){
}

而是使用 getUnknownsAccel(missingVar);

于 2013-07-13T18:07:10.523 回答