Java 如何检查值是否正确?
在我的setValue中,我在我的testclass中测试了一个错误,在我的testclass中setValue是0,因此它应该是00,但它在终端01中显示 我的老师将显示的值设置为newValue是什么意思,我尝试了这个.value;。然而,它仍然给我错误,此外,我试图改变它的符号,但老师说这是不正确的,我应该检查一下,如果值是正确的,然后设置为newValue。 我如何解决这个问题? 我感谢你的帮助Java 如何检查值是否正确?,java,oop,Java,Oop,在我的setValue中,我在我的testclass中测试了一个错误,在我的testclass中setValue是0,因此它应该是00,但它在终端01中显示 我的老师将显示的值设置为newValue是什么意思,我尝试了这个.value;。然而,它仍然给我错误,此外,我试图改变它的符号,但老师说这是不正确的,我应该检查一下,如果值是正确的,然后设置为newValue。 我如何解决这个问题? 我感谢你的帮助 private int minLimit; private int ma
private int minLimit;
private int maxLimit;
private int value;
private boolean wrapped;
public NumberDisplay(int minLimit, int maxLimit) throws IllegalValueException {
if (maxLimit > minLimit) {
this.minLimit = minLimit;
this.maxLimit = maxLimit - 1;
this.value = minLimit;
this.wrapped = true;
// creates new number display with the value minimum limit
// and the limits between maximum limit. if maximum limit is not greater than
// minimum limit
// it should throw exception
} else {
throw new IllegalValueException("felaktig");
}
}
public int getValue() {
return this.value;
// return the current value on display with integer number
}
public void setValue(int newValue) throws IllegalValueException {
if (newValue < minLimit || newValue > maxLimit) {
throw new IllegalValueException("Felaktig prova igen set time är felaktig");
// set the value on display to newValue. if newValue is less greater than minimum
// limit
// or greater than value from display then throw exception
} else {
if (newValue == minLimit) {
newValue = minLimit;
}
}
}
public String getDisplayValue() {
if (this.value < 11) {
return "0" + value;
} else {
return "" + value;
}
// return the current value with 0 infront of the value
}
public void increment() {
if (this.value == maxLimit) {
this.value = this.minLimit;
this.wrapped = true;
} else {
this.value = +1;
}
// increases the display with one value and checks if maximum limit is reached
// then
// it should be converted to minimum limit
// Consider to do the string delightful so all of values sets with as many as
// numbers
// the display should have. Values with low numbers should be zeros in front,
// e.g. clock 9 09:00
}
public boolean diWrapAround() {
if (this.value < maxLimit) {
return this.wrapped = true;
} else
return false;
}
// it should return true if the display reached over maximum limit and started
// from
// minimum limit , otherwise it should return false,
// use attribute wrapped to check boolean false or true.
}
私人有限公司;
私有整数最大限制;
私有int值;
私有布尔包;
public NumberDisplay(int minLimit,int maxLimit)抛出非法值异常{
如果(最大限制>最小限制){
this.minLimit=minLimit;
this.maxLimit=maxLimit-1;
this.value=minLimit;
这是真的;
//使用“最小限制”值创建新的数字显示
//以及最大限值之间的限值。如果最大限值不大于
//最低限度
//它应该抛出异常
}否则{
抛出新的非法ValueException(“felaktig”);
}
}
public int getValue(){
返回此.value;
//返回显示的当前值和整数
}
public void setValue(int newValue)抛出非法值异常{
if(newValuemaxLimit){
抛出新的非法值异常(“Felaktig prova igen set timeär Felaktig”);
//将显示的值设置为newValue。如果newValue小于最小值
//极限
//或大于显示的值,然后引发异常
}否则{
if(newValue==minLimit){
newValue=minLimit;
}
}
}
公共字符串getDisplayValue(){
如果(该值小于11){
返回“0”+值;
}否则{
返回“”+值;
}
//返回当前值,值前加0
}
公共空间增量(){
if(this.value==maxLimit){
this.value=this.minLimit;
这是真的;
}否则{
这个值=+1;
}
//用一个值增加显示,并检查是否达到最大限制
//然后
//应将其转换为最低限额
//考虑做字符串愉快,所以所有的值设置多达
//数字
//显示器应具有。数字较低的值前面应为零,
//例如,9点09分
}
公共布尔双环绕(){
如果(该值<最大限值){
返回this.wrapped=true;
}否则
返回false;
}
//如果显示超过最大限制并启动,则应返回true
//从
//最小限制,否则应返回false,
//使用属性包装检查布尔值false或true。
}
在此处输入代码public void setValue(int newValue)抛出非法值异常{
if(newValuemaxLimit){
抛出新的非法值异常(“Felaktig prova igen set timeär Felaktig”);
//将当前显示值设置为newValue。如果newValue小于或大于
//最低限度
//或大于显示的值,然后引发异常
}否则{
if(newValue==minLimit){
this.value=newValue;
}
}
}
对于想知道答案的人,我改变了这个;要检查
设置值中的值是否正确,您从未分配值
。如何检查该值是否正确?@AdamLord但您已经在if(newValuemaxLimit)
中进行了检查。你的问题是,在else块中,当值正确时,你什么也不做。是的,但是老师说,你应该另外检查值是否正确
enter code here public void setValue(int newValue) throws IllegalValueException {
if (newValue < minLimit || newValue > maxLimit) {
throw new IllegalValueException("Felaktig prova igen set time är felaktig");
// set the current display value to newValue. if newValue is less greater than
// minimum limit
// or greater than value from display then throw exception
} else {
if (newValue == minLimit ) {
this.value = newValue;
}
}
}