-3

我无法弄清楚我的代码要去哪里,也在我的 if 循环中,它不允许我在第一个循环之后输入更多公司名称。

分配4:

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

我们的日期:

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

1 回答 1

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
        }
    }
}
于 2015-12-03T17:34:54.067 回答