学长学姐帮忙看看--------我的这个java程序有什莫错呢?
public class SumDigits extends JavaKaraProgram
{
public void myProgram()
{
int x;
int y;
x=tools.intInput("Enter integer x:");
if(x>=100&&x<=999)
y=x/100+x%10+(x/10)%10;
else
if(x>=10&&x<100)
y=x/10+x%10;
else
if(x>=0&&x<10)
y=x;
else
tools.showMessage("Wrong entry.Try again.");
//
tools.showMessage("The result is"+y);
}
}
为什莫机器总是说the variable y may not have been initialized. ???????[tonymmm (8-26 23:29, Long long ago)]
[ 传统版 |
sForum ][登录后回复]1楼
come inlocal variable are not implicitly initialised and use of non-initialised local variables just arise the complie error.
here when the program follow goes to tools.showMessage("Wrong entry.Try again."); y is not initialised, so there is a possibility that y is used as in tools.showMessage("The result is"+y); without initialising it.
the simplest way to solve is to change int y; to int y = 0;
[想念老乡 (8-26 23:49, Long long ago)]
[ 传统版 |
sForum ][登录后回复]2楼
(引用 想念老乡:come inlocal variable are not implicitly initialised and use of non-initialised local variables just arise the complie error. h...)oooooo, thank u very very much!!!:):):)[tonymmm (8-26 23:53, Long long ago)] [ 传统版 | sForum ][登录后回复]3楼