试图了解为什么我的程序过早终止。可以运行加仑到升的转换方法,但是到此为止。不运行“根”方法(其目的是计算数字1到100的平方根)。我认为这更多是格式问题,而不是语义问题。谢谢你的帮助。

package gallons.to.liters;

public class converter {
public static void main(String args[]) {
    double gallons;
    double liters;

    gallons = 10;
    liters = gallons * 3.7854;

    System.out.println("The number of liters in " + gallons + " gallons is " +
            liters);
    System.out.println();

}


public static void root(String args[]) {
    double counter;
    double square;

    square = 0;
    counter = 0;

    for(square = 0; square <= 100; square++);
        square = Math.sqrt(square);
        counter++;
        System.out.println("The square root of " + counter + " is " +
                    square);



}
}

最佳答案

您永远不会调用root方法。将此添加到主要:

public static void main(String args[]) {
    double gallons;
    double liters;

    gallons = 10;
    liters = gallons * 3.7854;

    System.out.println("The number of liters in " + gallons + " gallons is " +
            liters);
    System.out.println();

    root(args); // ADD to call the method.
}

09-12 11:49