请帮助,我正在尝试根据从输入文件中读取的数量来计算书籍总数。我试图计算书籍的总数,但是现在我的代码只是生成它从文件中读取的最后一个数量,作为totalBooks的总数。我不确定我做错了什么,但任何指导都会非常感激。
这是我的......
注意:我尝试修复的程序部分位于注释// Calculate Total Books
之下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 :(得分:0)
您需要使用 public void toggleDirection(){
CustomMessageTreeNode currentSelectedNode =
(CustomMessageTreeNode) tcBuilderTree.getLastSelectedPathComponent();
Message toggleMessage=currentSelectedNode.getMessage();
tcBuilderTree.setCellRenderer(new DefaultTreeCellRenderer(){
@Override
public Component getTreeCellRendererComponent(JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean isLeaf,
int row,
boolean focused) {
Component c = super.getTreeCellRendererComponent(tree, value,
selected, expanded, isLeaf, row, focused);
if(value instanceof CustomMessageTreeNode){
String currentDirection=toggleMessage.getMsgDirection();
if(currentDirection.equalsIgnoreCase("In")){
toggleMessage.setMsgDirection("Out");
System.out.println("Direction changed as : Out");
setIcon(toggleMessage.getMessageDirectionIcon());
return c;
}
if(currentDirection.equalsIgnoreCase("Out")){
toggleMessage.setMsgDirection("In");
System.out.println("Direction changed as : In");
setIcon(toggleMessage.getMessageDirectionIcon());
return c;
}
}
return c;
}
});
}
您必须计算总数量,因此您需要通过添加下一个数量来更新以前的数量,而不是替换