2014-10-16 108 views
0

我試圖讓條件,其中邏輯/語義錯誤

  1. 如果發票數量小於1000,迫使發票號碼爲0
  2. 如果月份字段是小於1或大於12,強制月份字段爲0.
  3. 如果日期字段小於1或大於31,則強制日期字段爲0.
  4. 如果年份字段小於2011年或大於2017年,強制年份字段爲0.

但是當我嘗試運行它,它迫使所有字段爲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(); 
    } 

} 

回答

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() + "."); 
+0

謝謝,這工作! – Chrissy 2014-10-16 18:32:13

2

這是失敗,因爲你到displayInfo來電時您的通話將所有setter方法了。它需要首先發生,否則制定者沒有任何事情要做。

我認爲在構造函數中調用這些setter方法是錯誤的。一旦你有需要傳遞給它的值,你需要調用每一個。

所以,你可能會爲每個值,可以輸入,這你再傳給二傳手

int invoiceNumber = keyboard.nextInt(); 
setInNumber(invoiceNumber); 

等所有其他領域的局部變量。