Java总图书量计算
请帮忙,我正试图根据从输入文件读取的数量来计算书籍的总数。我曾试图计算书籍总数,但现在我的代码只是生成它从文件中读取的最后数量,作为totalBooks的总数。我不确定我做错了什么,但任何指导都将不胜感激 这是我所拥有的 注意:我试图修复的程序部分位于注释//计算总图书数下Java总图书量计算,java,Java,请帮忙,我正试图根据从输入文件读取的数量来计算书籍的总数。我曾试图计算书籍总数,但现在我的代码只是生成它从文件中读取的最后数量,作为totalBooks的总数。我不确定我做错了什么,但任何指导都将不胜感激 这是我所拥有的 注意:我试图修复的程序部分位于注释//计算总图书数下 import java.util.Scanner; import java.io.File; import java.io.IOException; public class BookstoreInventory { pu
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class BookstoreInventory
{
public static void main(String[] args)throws IOException
{
//Vaiable declartions
int edition, quanity;
double pricePerBook;
String isbn, author, title, publisherCode;
//Open the file and set delimiters
File file = new File("inventory.txt");
Scanner inputFile = new Scanner(file);
inputFile.useDelimiter("_|/|\\r?\\n");
//read from the file
while (inputFile.hasNext())
{
isbn = inputFile.next();
System.out.println(formatISBN(isbn)); //Need to move at end
author = inputFile.next();
System.out.println(getLastName(author)); //need to remove at end
title = inputFile.next();
System.out.println(truncateTitle(title)); //need to remove at end
edition = inputFile.nextInt();
System.out.println(edition(edition)); //need to remove at end
publisherCode = inputFile.next();
System.out.println(publisher(publisherCode)); //need to remove at end
quanity = inputFile.nextInt();
System.out.println(quanity); //need to remove at end
pricePerBook = inputFile.nextDouble();
System.out.println("$" + pricePerBook); //need to remove at end
//Calculate Total Books
int totalBooks = 0;
totalBooks += quanity;
System.out.println("Total books: " + totalBooks);
}
//Close the flie
inputFile.close();
}
您需要使用
quantity+=inputFile.nextInt()代码>
您必须计算总数量,因此您需要通过添加下一个数量而不是替换来更新以前的数量