警告 – 字段EggBox.boxSize的值未被使用

huangapple go评论71阅读模式
英文:

Warning - The value of the field EggBox.boxSize is not used

问题

I see that you have provided a code snippet along with your question. You're encountering warnings related to the "boxSize" variable and some methods in your code. Here's the translation of the relevant information:

你提供了一个代码片段,并且遇到了与“boxSize”变量以及一些方法相关的警告。以下是相关信息的翻译:

  • The value of the field EggBox.boxSize is not used.

    • EggBox.boxSize字段的值未被使用。
  • Multiple markers at this line - Line breakpoint:EggBox [line: 12] - setboxSize(int) - boxSize cannot be resolved or is not a field.

    • 此行有多个标记 - 行断点:EggBox [行:12] - setboxSize(int) - 无法解析boxSize或不是字段。

These warnings indicate that the "boxSize" variable in your code is not being used effectively. To resolve these issues:

这些警告表明你的代码中的“boxSize”变量没有被有效使用。要解决这些问题:

  1. Ensure that you are using the "boxSize" variable in your code where it is needed.

确保你在代码中在需要的地方使用“boxSize”变量。

  1. Double-check the method names and parameters to make sure they match between the class and the calling code. In your code, there are discrepancies between method names and parameters.

仔细检查方法名和参数,确保它们在类和调用代码之间匹配。在你的代码中,方法名和参数之间存在差异。

  1. In your code, you have defined the "EggBox" class twice. You should have only one definition of the class.

在你的代码中,你定义了“EggBox”类两次。你应该只有一个类的定义。

By addressing these issues, you should be able to resolve the warnings and have your code work as expected. If you have any specific questions or need further assistance with your code, feel free to ask.根据这些问题,你应该能够解决警告并让你的代码按预期工作。如果你有任何具体问题或需要进一步的帮助,随时提问。

英文:

I am working on Instantiable classes. I have wrote my code on two separate files but I see the following warning on my EggBox file - The value of the field EggBox.boxSize is not used. However the program runs as expected and I still get the input methods for the user.

If I delete the variable int boxSize then I end up with this warning - Multiple markers at this line- Line breakpoint:EggBox [line: 12] - setboxSize(int)- boxSize cannot be resolved or is not a field

This is an exercise of filling in the blanks that I am trying to complete. It is the last piece of code

The eggBoxApp has no error or warnings

import javax.swing.JOptionPane;
public class EggBoxApp{
	public static void main(String[] args){
		//variables
		int eggs;
		int boxSize;
		int numBoxes;
		int leftOverEggs;
		//objects
		EggBox egg;
		egg=new EggBox();
		//inputs
		eggs=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter a number of eggs"));
		boxSize=6;
		//set
		egg.setEggs(eggs);
		egg.setboxSize(boxSize);
		//compute
		egg.computeBoxes();
		egg.computeLeftOver();
		//get
		eggs=egg.getBoxes();
		eggs=egg.getLeftOver();
		//output
		JOptionPane.showMessageDialog(null,"Number of boxes needed "+numBoxes);
		JOptionPane.showMessageDialog(null,"Number of eggs left over "+leftOverEggs);
	}
}

public class EggBox{
	//vars
	private int eggs;
	private int boxSize;
	private int numBoxes;
	private int leftOverEggs;
	//set
	public void setEggs(int eggs){
		this.eggs=eggs;
	}
	public void setboxSize(int boxSize){
		this.boxSize=boxSize;
	}
	//compute
	public void computeBoxes(){
		numBoxes=eggs/6;
	}
	public void computeLeftOver(){
		leftOverEggs=eggs%6;
	}
	//get
	public int getBoxes(){
		return numBoxes;
	}
	public int getLeftOver(){
		return leftOverEggs;
	}
}

public class EggBox{
	//vars
	**** int eggs;
	**** **** boxSize;
	**** **** numBoxes;
	**** **** leftOverEggs;
	//set
	public **** setEggs(int eggs){
		this.eggs=eggs;
	}
	public **** setBoxSize(int ****){
		this.****=****;
	}
	//compute
	public void computeBoxes(){
		numBoxes=eggs/6;
	}
	public void computeLeftover(){
		leftOverEggs=****;
	}
	//get
	public **** getBoxes(){
		return boxes;
	}
	public **** getLeftover(){
		return leftover;
	}
}

I understand it is a warning but I am not sure how to proceed or fix it. Any help explaining this would be great. Thanks

答案1

得分: 2

你正在设置属性boxSize的值,但在设置后从未使用过。

这个警告是基于这样一个事实的,如果你设置了一个变量,你可能打算使用它,对吗?

如果你只是想让警告消失,你可以添加一些getter方法(例如getBoxSize()),它会返回你的属性的值,然后调用它。

英文:

You are setting the value of the attribute boxSize, but after setting it you never use it.

The warning is based on the fact that if you set a variable, you probably intend to use it, right ?

If you just want to make the warning disappear, you could add some getter (like getBoxSize()) that would return the value of your attribute, and call it.

huangapple
  • 本文由 发表于 2023年4月17日 18:12:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定