java question

2008-10-28 3:54 am
boolean leap_year;
int year,m;
String month,year2,answer;
month = JOptionPane.showInputDialog("Enter a month");
m=Integer.parseInt(month);
switch(m)
{
case 1: answer="The number of days on month Jan is: 31";
break;
case 2:
year2= JOptionPane.showInputDialog("Enter a year");
year=Integer.parseInt(year2);
if (leap_year=( ((year%4==0)&&(year%100!=0))||(year%400==0) ))
answer="The number of days on month Feb and year "+year+" is 29";
else
answer="The number of days on month Feb and year "+year+" is 28";
break;
case 3:
answer="The number of days on month Mar is: 31";
break;
case 4:
answer="The number of days on month App is: 30";
break;
case 5:
answer="The number of days on month May is: 31";
break;
case 6:
answer="The number of days on month Jun is: 31";
break;
case 7:
answer="The number of days on month Jul is: 31";
break;
case 8:
answer="The number of days on month Aug is: 31";
break;
case 9:
answer="The number of days on month Sep is: 30";
break;
case 10:
answer="The number of days on month Oct is: 31";
break;
case 11:
answer="The number of days on month Nev is: 30";
break;
case 12:
answer="The number of days on month Dec is: 31";
break;
}
JOptionPane.showMessageDialog(null,answer,"result",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);

number.java:55: variable answer might not have been initialized
JOptionPane.showMessageDialog(null,answer,"result",JOptionPane.INFORM
^
ATION_MESSAGE);

1 error
how can i solve the problem?

回答 (2)

2008-10-28 5:05 pm
✔ 最佳答案
String month,year2="",answer="";

> number.java:55: variable answer might not have been initialized
means that the variable was initialised/assigned inside of a conditional statement, whichh does not guarantee that it has a value when it is used/referenced.
The usual technique is to initialize it to something when you declare it, as in above.
If you need more details, PM me.

2008-10-28 10:58:21 補充:
You do not have to hate people if they do not give you the right answer. They are trying to help you.

2008-10-29 11:41:15 補充:
I understand the frustration! Also it occupies one answer, which makes some other people not even look at your question.
How did they go away?
2008-10-28 10:52 pm
I am sorry. Since someone posted some advertisements that it is not I want ........

2008-10-29 19:34:18 補充:
I told the manager of Yahoo. Then ,they were not to appear in my question


收錄日期: 2021-04-13 16:12:25
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20081027000051KK01710

檢視 Wayback Machine 備份