[英]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 循環。 內部 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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.