gpt4 book ai didi

Java 从类 getValue 调用

转载 作者:行者123 更新时间:2023-11-30 05:04:29 26 4
gpt4 key购买 nike

好吧,我正在尝试创建一个名为:setIncrement() 的方法。我正在使用两个类,FourDigit,其中包含我制作的两个 TwoDigit 对象。我将每个称为segment1 和segment2。他们的关系是:segment1.segment2。该方法将增加segment2,当它重置为零时,该方法将增加segment1。

问题出在这段代码中:

 public void setIncrement(){
segment2.incrementValue();
if(segment2.getValue == 0){
segment1.value = segment1.value +1;
}else{
segment2=aNum%tooHighB;
}
getDisplayString();
}

它说:找不到符号变量:getValue() ???

以下是类(class):

四位数字类:

public class FourDigits
{
/**
* Fields
*/
private Digits segment1;
private Digits segment2;
private String displayString;

/**
* Constructor for objects of class FourDigits
*/
public FourDigits(int anyHigha, int anyHighb)
{
segment1 = new Digits(anyHigha);
segment2 = new Digits(anyHighb);
}

/**
* Mutator method to set values
*/
public void setValues(int anyNuma, int anyNumb){
segment1.setValue(anyNuma);
segment2.setValue(anyNumb);
}
/**
* Mutator method to set the increments
*/
public void setIncrement(){
segment2.incrementValue();
if(segment2.getValue == 0){
segment1.value = segment1.value +1;
}else{
segment2=aNum%tooHighB;
}
getDisplayString();
}
/**
* Mutator method to getDisplayString
*/
public void getDisplayString(){
System.out.println(segment1.displayString() ="." + segment2.displayString());
}
}

它从 Digits 类中获取方法:

public class Digits
{
/**
* Fields
*/
private int value;
private int tooHigh;
private String displayString;

/**
* Constructors
*/
public Digits(int anyNum)
{
value = 0;
tooHigh=anyNum;
displayString = "";
}
/**
* Mutator method to set the value
*/
public void setValue(int anyValue){
if((anyValue < tooHigh) && (anyValue >= 0)){
value = anyValue;
}else{
value=0;
}
}
/**
* Mutator method to add to the value
*/
public void addValue(){
if(value < tooHigh){
value=value+1;
}else{
value=tooHigh;
}
}
/**
* Mutator method to increment the value
*/
public void incrementValue(){
int incrementValue;
incrementValue = value + 1;
value = incrementValue % tooHigh;
}
/**
* Mutator method to display the string
*/
public void displayString(){
if(value<10){
displayString="0" + value;
}else{
displayString="" + value;
}
System.out.println(displayString);
}
public int getValue(){
return value;
}

}

最佳答案

除了使用赋值运算符而不是相等比较之外,您还尝试设置segment2而不是segment2.value。我怀疑该 block 应该是:

if (segment2.incrementValue() == 0) {
segment1.setValue(segment1.getValue() + 1);
} else {
segment2.setValue(aNum % tooHighB);
}

请注意,我看不到任何 aNumtooHighB 声明...并且 incrementValue() 目前返回 void ,而您可能希望它返回新值。

(顺便说一下,我鼓励您使用空格来使代码更易于阅读。)

关于Java 从类 getValue 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5586967/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com