0
我試圖讓條件,其中邏輯/語義錯誤
- 如果發票數量小於1000,迫使發票號碼爲0
- 如果月份字段是小於1或大於12,強制月份字段爲0.
- 如果日期字段小於1或大於31,則強制日期字段爲0.
- 如果年份字段小於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();
}
}
謝謝,這工作! – Chrissy 2014-10-16 18:32:13