逻辑/语义错误

时间:2014-10-16 18:14:45

标签: java html java.util.scanner

我试图创造条件

  1. 如果发票号码小于1000,请强制将发票号码设为0。
  2. 如果月份字段小于1或大于12,则强制将月份字段设为0。
  3. 如果日期字段小于1或大于31,则强制将日期字段设为0。
  4. 如果年份字段小于2011年或大于2017年,请将年份字段强制为0。
  5. 但是当我尝试运行它时,它会强制所有字段为0,即使我为变量设置了正确的值:

    Please type your invoice number: ...1111
    What month is the balance due?(Choose from 1-12)... 1
    What day is the balance due?(Choose from 1-31).... 12
    What year is the balance due? (Choose from 2011-2017) ....2011
    Your invoice number is 0 and your balance due is on 0/0/0.
    
    public class Invoice
    {
        private int     inNum;
        private double  balDue;
        private int     m;
        private int     d;
        private int     yy;
    
        public Invoice ()
        {
            super();
            setInNum(inNum);
            getInNum();
            setBalDue(balDue);
            setM(m);
            setD(d);
            setYy(yy);
    
        }
    
        public int getInNum ()
        {
            return inNum;
        }
    
        public void setInNum (int inNum)
        {
            if (inNum < 1000)
                this.inNum = 0;
            else
                this.inNum = inNum;
        }
    
        public double getBalDue ()
        {
            return balDue;
        }
    
        public void setBalDue (double balDue)
        {
            this.balDue = balDue;
        }
    
        public int getM ()
        {
            return m;
        }
    
        public void setM (int m)
        {
            if (m < 1 || m > 12)
                this.m = 0;
            else
                this.m = m;
        }
    
        public int getD ()
        {
            return d;
        }
    
        public void setD (int d)
        {
            if (d < 1 || d > 31)
                this.d = 0;
            else
                this.d = d;
    
        }
    
        public int getYy ()
        {
            return yy;
        }
    
        public void setYy (int yy)
        {
            if (yy < 2011 || yy > 2017)
                this.m = 0;
            else
                this.yy = yy;
        }
    
        public void displayInfo ()
        {
            int inNum;
    
            int m, d, yy;
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Please type your invoice number: ");
            inNum = keyboard.nextInt();
            System.out
                    .print("What month is the balance due?(Choose from 1-12) ");
            m = keyboard.nextInt();
            System.out
                    .print("What day is the balance due?(Choose from 1-31) ");
            d = keyboard.nextInt();
            System.out
                    .print("What year is the balance due? (Choose from 2011-2017) ");
            yy = keyboard.nextInt();
    
            System.out.println("Your invoice number is " + getInNum()
                    + " and your balance due is on " + getM() + "/" + getD()
                    + "/" + getYy() + ".");
    
        }
    
        public static void main (String[] args)
        {
            Invoice invoice = new Invoice();
            invoice.displayInfo();
        }
    
    }
    

2 个答案:

答案 0 :(得分:2)

这是失败的,因为您在调用所有setter方法后调用了displayInfo。它需要先发生,否则制定者无所事事。

我认为在构造函数中调用这些setter方法是错误的。一旦你有了需要传递给它的值,你需要调用每一个。

因此,您输入的每个值都可能有一个局部变量,然后将其传递给setter

int invoiceNumber = keyboard.nextInt();
setInNumber(invoiceNumber);
所有其他领域的

等等。

答案 1 :(得分:1)

你为这个班制作了所有那些漂亮的制定者并且没有使用它们!

Scanner keyboard = new Scanner(System.in);
System.out.print("Please type your invoice number: ");
setInNum(keyboard.nextInt());
System.out.print("What month is the balance due?(Choose from 1-12) ");
setM(keyboard.nextInt());
System.out.print("What day is the balance due?(Choose from 1-31) ");
setD(keyboard.nextInt());
System.out.print("What year is the balance due? (Choose from 2011-2017) ");
setY(keyboard.nextInt());

System.out.println("Your invoice number is " + getInNum() +  " and your balance due is on " + getM() + "/" + getD() + "/" + getYy() + ".");