我的程序应该要求某人输入序列号和对象的价格。
这是类驱动程序
import java.io.*;
import java.util.Scanner;
public class InventoryDriver {
public static void main(String[] args)throws IOException {
Inventory storeItem = new Inventory(" ", 1);
PrintWriter storeFile = new PrintWriter("StoreInventory.txt");
Scanner scan = new Scanner(System.in);
boolean running = true;
String itemNum = " ";
double originalPrice = 1;
while (running) {
System.out.println("Enter the item number or enter 0 when you are done.");
itemNum = scan.nextLine();
storeItem.setItemNum(itemNum);
storeFile.println(itemNum);
if(itemNum.equals("0")) break;
System.out.println("Enter the original price or enter 0 when you are done.");
originalPrice = scan.nextDouble();
storeItem.setOriginalPrice(originalPrice);
storeFile.println(originalPrice);
if(originalPrice == 0) break;
}
//create a scanner variable named myFile
Scanner myFile = new Scanner(new File("StoreInventory.txt"));
//Read my data from my file into the variablles
//itemNum = myFile.nextLine();
//originalPrice = myFile.nextDouble();
printSaleData(myFile, storeItem);
//Close the file
myFile.close();
storeFile.close();
}
}
第一次使用此输出完美工作后
Enter the item number or enter 0 when you are done.
input
Enter the original price or enter 0 when you are done.
input
但是当循环返回时,它会打印出这个
Enter the item number or enter 0 when you are done.
input
Enter the original price or enter 0 when you are done.
input
Enter the item number or enter 0 when you are done.
Enter the original price or enter 0 when you are done.
input
我的代码在第一次运行后如何更改循环? 附:即使我打破了循环,整个程序也会停止,并且不会调用" printSaleData"子模块到主模块。
答案 0 :(得分:0)
将scan.nextLine更新为scan.next()。这样就可以了。