我不明白为什么我的程序没有循环

I can't figure out why my program isn't looping

我试图让这个文件循环,但我在最后一个 if 语句(用箭头标记)上总是出错。

程序应该读取第一行是客户名称的文件。
在第二行,第一个数字是要移除的树的数量(每棵树 150 棵),第二个数字是要修剪的树(每小时 50 棵)。
第三行是所有要移除的树桩和它们的直径(一个数字是一个树桩,也是它的直径)。

这是应该读入的文件 (http://pastebin.com/gXkujcaM)。

public class Prog_5 {

    public static void main(String[] args) throws FileNotFoundException {
        String name = "Joe";
        double trees = 0;
        double treeTrimming = 0;
        double stumpInches = 0;
        double stumpTotal = 0;
        double total = 0;
        double totalRev = 0;

        Scanner in = new Scanner(System.in);
        System.out.print("Input file: ");
        String inputFile = in.nextLine();

        System.out.print("Output file: ");
        String outputFile = in.nextLine();
        in.close();

        File input = new File(inputFile);
        in = new Scanner(input);
        PrintWriter output = new PrintWriter(outputFile);

        while(in.hasNext()){

        name = in.nextLine();
        output.println("Customer: " + name);
        System.out.println("Customer: " + name);

        trees = in.nextDouble();
        trees *= 150;
        output.println("Tree Removal: $" + trees);
        System.out.println("Tree Removal: $" + trees);

        treeTrimming = in.nextDouble();
        treeTrimming *= 50;
        output.println("Tree Trimming: $" + treeTrimming);
        System.out.println("Tree Trimming: $" + treeTrimming);

        while (in.hasNextDouble()) {
            stumpInches = in.nextDouble();
            if (stumpInches != -1) {
                stumpTotal = stumpTotal + 30;
                if (stumpInches > 12) {
                    stumpInches -= 12;
                    stumpInches *= 2;
                }
                stumpTotal += stumpInches;
            }

        }
        output.println("Stump Removal: $" + stumpTotal);
        System.out.println("Stump Removal: $" + stumpTotal);

        total = (trees + treeTrimming + stumpTotal);
        output.println("Total: $" + total);
        System.out.println("Total: $" + total);

        totalRev += total;
        stumpTotal = 0;
        trees = 0;
        treeTrimming = 0;

        if(in.hasNext());
            in.next();
        }

        output.close();

    }

}

是你的 if 循环有问题。你应该像下面这样写。但是你用 ;

终止了它
if(in.hasNext()){
       in.next();
        }

因此每次到达 EOF 时都会继续抛出 nosuchelement 异常

不需要外部while循环,if条件在最后。内部 while 循环负责处理您的程序打算执行的操作。 PFB 更正代码:

import java.io.*;
import java.util.Scanner;

public class Prog_5 {

    public static void main(String[] args) throws FileNotFoundException {
        String name = "Joe";
        double trees = 0;
        double treeTrimming = 0;
        double stumpInches = 0;
        double stumpTotal = 0;
        double total = 0;
        double totalRev = 0;

        Scanner in = new Scanner(System.in);
        System.out.print("Input file: ");
        String inputFile = in.nextLine();

        System.out.print("Output file: ");
        String outputFile = in.nextLine();
        in.close();

        File input = new File(inputFile);
        in = new Scanner(input);
        PrintWriter output = new PrintWriter(outputFile);

        // while (in.hasNext()) {

        name = in.nextLine();
        output.println("Customer: " + name);
        System.out.println("Customer: " + name);

        trees = in.nextDouble();
        trees *= 150;
        output.println("Tree Removal: $" + trees);
        System.out.println("Tree Removal: $" + trees);

        treeTrimming = in.nextDouble();
        treeTrimming *= 50;
        output.println("Tree Trimming: $" + treeTrimming);
        System.out.println("Tree Trimming: $" + treeTrimming);

        while (in.hasNextDouble()) {
            stumpInches = in.nextDouble();
            if (stumpInches != -1) {
                stumpTotal = stumpTotal + 30;
                if (stumpInches > 12) {
                    stumpInches -= 12;
                    stumpInches *= 2;
                }
                stumpTotal += stumpInches;
            }

        }
        output.println("Stump Removal: $" + stumpTotal);
        System.out.println("Stump Removal: $" + stumpTotal);

        total = (trees + treeTrimming + stumpTotal);
        output.println("Total: $" + total);
        System.out.println("Total: $" + total);

        totalRev += total;
        stumpTotal = 0;
        trees = 0;
        treeTrimming = 0;

        // if (in.hasNext())
        // ;
        // in.next();
        // }
        in.close();
        output.close();

    }
}