Java 当循环在第一次迭代后跳过第一个输入扫描程序时?
我的程序应该要求某人输入一个序列号和一个对象的价格 这是班级司机Java 当循环在第一次迭代后跳过第一个输入扫描程序时?,java,while-loop,module,Java,While Loop,Module,我的程序应该要求某人输入一个序列号和一个对象的价格 这是班级司机 import java.io.*; import java.util.Scanner; public class InventoryDriver { public static void main(String[] args)throws IOException { Inventory storeItem = new Inventory(" ", 1); PrintWriter store
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”子模块调用到主模块。
将scan.nextLine更新为scan.next()。这样就可以了。
您能确切地说明您为获得这些结果而输入的内容吗?您要查找的输出到底是什么?可能更改itemNum=scan.nextLine()
toitemNum=scan.nextInt()代码>?