2015-12-03 60 views
-3

我無法弄清楚我的代碼在哪裏,也在我的if循環中,它不允許我在第一個循環後輸入更多的公司名稱。需要幫助使用數組創建發票程序

Assign4:

import java.util.Scanner; 

public class Assign4 { 

    private static int invoice; 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     OurDate date = new OurDate(); 
     Invoice inv = new Invoice(); 
     Ledger led = new Ledger(); 

     System.out.print("Enter the amount of monthly invoices: "); 
     invoice = scan.nextInt(); 

     for(int i = 0; i < invoice; i++){ 

      System.out.println("\nEnter info for invoice number " + i); 
      inv.setCompanyNameFromUser(); 
      inv.toString(); 
      inv.setBillAmountFromUser(); 
      inv.setDateFromUser(); 
      date.setDayFromUser(); 
      date.setMonthFromUser(); 
      date.setYearFromUser(); 
     } 
    } 
} 

OurDate:

import java.util.Scanner; 

public class OurDate { 

    private int day; 
    private int month; 
    private int year; 
    Scanner scan = new Scanner(System.in); 

    public OurDate(){ 
     day = 0; 
     month = 0; 
     year = 1900; 
    } 
    public void setDayFromUser(){ 
     System.out.print("Enter day : "); 
     day = scan.nextInt(); 
    } 
    public void setMonthFromUser(){ 
     System.out.print("Enter month : "); 
     month = scan.nextInt(); 
    } 
    public void setYearFromUser(){ 
     System.out.print("Enter year : "); 
     year = scan.nextInt(); 
    } 
    public String toString(){ 
     return null; 

    } 
} 

發票:

import java.util.Scanner; 

public class Invoice { 

    private double billAmount; 
    private String name; 
    private OurDate dueDate; 
    Scanner scan = new Scanner(System.in); 

    public Invoice(){ 
     billAmount = 0; 
    } 
    public void setDateFromUser(){ 
     System.out.print("Enter invoice due date\n"); 
     //dueDate = scan.nextInt(); 
    } 
    public void setCompanyNameFromUser(){ 
     System.out.print("Enter the company name : "); 
     name = scan.nextLine(); 
    } 
    public void setBillAmountFromUser(){ 
     System.out.print("Enter bill amount : "); 
     billAmount = scan.nextDouble(); 
    } 
    public String toString(){ 
     return name; 

    } 

} 

總帳:

import java.util.Scanner; 

public class Ledger { 

    private Invoice [] invoices; 
    private int numInvoices; 
    Scanner scan = new Scanner(System.in); 

    public Ledger(){ 

    } 
    /*public Ledger(int){ 

    }*/ 
    public void getInvoiceInfo(){ 
     System.out.println("Enter info for the invoice number 0\n"); 
    } 
    public void printInvoiceInfo(){ 

    } 
    public double calculateMonthBills(){ 
     return numInvoices; 

    } 
} 
+4

什麼是「if循環」? –

+0

也許一個案例http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods –

+1

你應該解釋你的代碼失敗的具體步驟。那麼你應該詳細描述你是如何解決這個問題的。然後,您應該描述哪些解決方案無效。然後你應該問一個具體和可回答的問題。 – csmckelvey

回答

1

由於Manos Nikolaidis提到的鏈接指出,您需要調用#nextLine()以跳過包含在發票對象中的Scanner對象上的#readInt()之後的\ n。

下面的修改後的代碼應該可以解決您的問題。

import java.util.Scanner; 

public class Assign4 { 

    private static int invoice; 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     OurDate date = new OurDate(); 
     Invoice inv = new Invoice(); 
     Ledger led = new Ledger(); 

     System.out.print("Enter the amount of monthly invoices: "); 
     invoice = scan.nextInt(); 

     for(int i = 0; i < invoice; i++) { 

      System.out.println("\nEnter info for invoice number " + i); 
      inv.setCompanyNameFromUser(); 
      inv.toString(); 
      inv.setBillAmountFromUser(); 
      inv.setDateFromUser(); 
      date.setDayFromUser(); 
      date.setMonthFromUser(); 
      date.setYearFromUser(); 
      inv.scan.nextLine(); // Need this line so \n is skipped 
     } 
    } 
}