需要帮助修复java项目上的错误吗

ybzsozfc  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(387)

**结案。**此问题不可复制或由打字错误引起。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

上个月关门了。
改进这个问题
这是我的密码。

  1. public class Main {
  2. public static void main(String[] args) {
  3. public class Shape{
  4. private String color;
  5. private boolean filled;
  6. public setDefault(){
  7. color = green;
  8. filled = true;
  9. }
  10. public Shape(String color, boolean filled){
  11. this.color = color;
  12. this.filled = filled;
  13. }
  14. public String getColor(){
  15. return color;
  16. }
  17. public void setColor(String newColor){
  18. this.color = newColor;
  19. }
  20. public boolean getFilled(){
  21. return filled;
  22. }
  23. public void setFilled(boolean newFilled){
  24. this.filled = newFilled;
  25. }
  26. public void toString(){
  27. System.out.println("A Shape with color of: " + color + "and" + filled);
  28. }
  29. }
  30. Shape test = new Shape();
  31. test.toString();
  32. }
  33. }

基本上,我正在努力使它可以打印“一个绿色和填充颜色的形状。这是我的错误。

  1. exit status 1
  2. Main.java:5: error: illegal start of expression
  3. public class Shape{
  4. ^
  5. Main.java:9: error: invalid method declaration; return type required
  6. public setDefault(){
  7. ^

不过,我对java很生疏,我知道还有更多的错误。有人能指出并告诉我问题是什么,我应该写些什么来解决它吗?

ryhaxcpt

ryhaxcpt1#

1:您试图在一个无效的方法中定义一个公共类:如果您想定义一个公共类,那么您必须按名称为它创建另一个文件 Shape 或者如果你想在里面定义它 Main 类只需在主类的作用域中定义它,您必须像这样访问它: new Main().new Shape("color", true); 2: setDefault() 是一个方法,因此它需要一个返回类型,如果不返回任何值,则需要将其设置为 void .
3:字符串值始终使用“”(双引号)。
4:重写默认构造函数,因此调用new时必须传递形状的参数 new Shape(); :
5:您覆盖了 toString() 方法,它的返回类型为 String 你必须按照它的定义来做:

  1. public class Main {
  2. public static void main(String[] args) {
  3. Shape test = new Shape("color", true);
  4. System.out.println(test.toString());
  5. }
  6. }
  7. class Shape {
  8. private String color;
  9. private boolean filled;
  10. public void setDefault() {
  11. color = "green";
  12. filled = true;
  13. }
  14. public Shape(String color, boolean filled) {
  15. this.color = color;
  16. this.filled = filled;
  17. }
  18. public String getColor() {
  19. return color;
  20. }
  21. public void setColor(String newColor) {
  22. this.color = newColor;
  23. }
  24. public boolean getFilled() {
  25. return filled;
  26. }
  27. public void setFilled(boolean newFilled) {
  28. this.filled = newFilled;
  29. }
  30. public String toString() {
  31. return "A Shape with color of: " + color + "and" + filled;
  32. }
  33. }
展开查看全部
hi3rlvi2

hi3rlvi22#

您需要定义一个单独的类 Shape 并返回一个 StringtoString() . 也, setDefault 应该是没有参数的构造函数:
main.java文件:

  1. public class Main {
  2. public static void main(String[] args) {
  3. Shape test = new Shape();
  4. System.out.println(test.toString());
  5. }
  6. }

shape.java文件:

  1. public class Shape{
  2. private String color;
  3. private boolean filled;
  4. public Shape(){
  5. this.color = "green";
  6. this.filled = true;
  7. }
  8. public Shape(String color, boolean filled){
  9. this.color = color;
  10. this.filled = filled;
  11. }
  12. public String getColor(){
  13. return color;
  14. }
  15. public void setColor(String newColor){
  16. this.color = newColor;
  17. }
  18. public boolean getFilled(){
  19. return filled;
  20. }
  21. public void setFilled(boolean newFilled){
  22. this.filled = newFilled;
  23. }
  24. public String toString(){
  25. return "A Shape with color of: " + this.color + " and " + this.filled;
  26. }
  27. }
展开查看全部
sg24os4d

sg24os4d3#

首先:您试图在方法内部定义一个类,这是不允许的(匿名类除外)。
最简单的修复方法是将类声明从 main 方法通过移动 main 你班后面的方法。
第二:你的 setDefault() 方法必须具有返回类型。如果你不打算归还任何东西,请使用 void .

相关问题