需要帮助使用数组创建发票程序

时间:2015-12-03 16:33:40

标签: java arrays invoice invoices

我无法弄清楚我的代码在哪里,也在我的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;

    }   
}

1 个答案:

答案 0 :(得分:1)

正如Manos Nikolaidis所提到的链接所示,您需要调用#nextLine()跳过Invoice对象中包含的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
        }
    }
}