2016-04-12 154 views
0

请帮忙,我试图根据从输入文件中读取的数量来计算书籍总数。我试图计算书的总数,但现在我的代码只是产生它从文件中读取的最后一个数量,作为totalBooks的总数。我不确定我做错了什么,但任何指导都将不胜感激。Java总计书籍计算

这是我有...

注:我的节目,我试图解决的部分是在评论//计算总书

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(); 
} 

回答

0

您需要使用quantity+= inputFile.nextInt();

您必须计算总量,因此您需要通过添加下一个数量来更新以前的数量,而不用替代