【问题标题】:Errors when trying to compile java file尝试编译 java 文件时出错
【发布时间】:2016-04-11 09:20:46
【问题描述】:

我在尝试使用 java 编译时遇到了几个错误。这是我关于堆栈溢出的第一篇文章,所以请指出我的帖子中的错误。

代码是我为实用而制作的一些测试代码。

以下是我的代码:

public class MiscUtilsTest
{

    public static void main(String[] args)
    {
    testMax();
    testCalcGrade();
    testRoomArea();
    }

    public static void testMax()
    {
    int num, num2, num3;

    num = MiscUtils.max(9, 2);
    assert 9 == num;

    num2 = MiscUtils.max(6, 1);
    assert 6 == num2;

    num3 = MiscUtils.max(7, 7);
    assert 7 == num3;

    }

    public static void testCalcGrade()
    {
    int grade1, grade2, grade3, grade4, grade5, grade6, grade7, grade8, grade9;

    grade1 = MiscUtils.calcGrade(2);
    assert "F".equals(grade1);

    grade2 = MiscUtils.calcGrade(15);
    assert "F".equals(grade2);

    grade3 = MiscUtils.calcGrade(28);
    assert "F".equals(grade3);

    grade4 = MiscUtils.calcGrade(33);
    assert "F".equals(grade4);

    grade5 = MiscUtils.calcGrade(40);
    assert "F".equals(grade5);

    grade6 = MiscUtils.calcGrade(49);
    assert "F".equals(grade6);

    grade7 = MiscUtils.calcGrade(82);
    assert "8".equals(grade7);

    grade8 = MiscUtils.calcGrade(125);
    assert "".equals(grade8);

    grade9 = MiscUtils.calcGrade(-12);
    assert "".equals(grade9);

    }

    public static void testRoomArea()
    {
    int test, test2, test3;

    test2 = MiscUtils.roomArea(-10, 5);
    assert 0 == test;

    test2 = MiscUtils.roomArea(6, 2);
    assert 0 == test2;

    test3 = MiscUtils.roomArea(5, 10);
    assert 50 == test3;

    }
}

以下是我在编译时遇到的错误-

MiscUtilsTest.java:45: error: cannot find symbol
MiscUtilsTest.java:30: error: incompatible types: String cannot be converted to int
    grade1 = MiscUtils.calcGrade(2);
                                ^
MiscUtilsTest.java:33: error: incompatible types: String cannot be converted to int
    grade2 = MiscUtils.calcGrade(15);
                                ^
MiscUtilsTest.java:36: error: incompatible types: String cannot be converted to int
    grade3 = MiscUtils.calcGrade(28);
                                ^
MiscUtilsTest.java:39: error: incompatible types: String cannot be converted to int
    grade4 = MiscUtils.calcGrade(33);
                                ^
MiscUtilsTest.java:42: error: incompatible types: String cannot be converted to int
    grade5 = MiscUtils.calcGrade(40);
                                ^
MiscUtilsTest.java:45: error: incompatible types: String cannot be converted to int
    grade6 = MiscUtils.calcGrade(49);
                                ^
MiscUtilsTest.java:48: error: incompatible types: String cannot be converted to int
    grade7 = MiscUtils.calcGrade(82);
                                ^
MiscUtilsTest.java:51: error: incompatible types: String cannot be converted to int
    grade8 = MiscUtils.calcGrade(125);
                                ^
MiscUtilsTest.java:54: error: incompatible types: String cannot be converted to int
    grade9 = MiscUtils.calcGrade(-12);
                                ^
9 errors

对于那些询问类文件的人,来自 MiscUtils.class 的代码

import java.io.PrintStream;
import java.io.PrintStream;

public class MiscUtils
{
static
{
    System.out.println("[FYI: You are testing the *working* version of MiscUtils. Your test harness *should not* find any problems.]");
 }

public static int max(int paramInt1, int paramInt2)
{
    int i = paramInt1;
    if (paramInt2 > paramInt1) {
    i = paramInt2;
    }
    return i;
}

public static String calcGrade(int paramInt)
{
    String str = "";
    if ((paramInt >= 0) && (paramInt <= 100)) {
    if (paramInt >= 50) {
    str = str + paramInt / 10;
    } else {
    str = "F";
    }
    }
    return str;
}

public static int roomArea(int paramInt1, int paramInt2)
{
    int i = 0;
    if ((paramInt1 > 0) && (paramInt2 >= paramInt1) && (paramInt2 <= 3 * paramInt1)) {
    i = paramInt1 * paramInt2;
 }
    return i;
}
}

【问题讨论】:

  • 对于初学者,您也可以发布MiscUtils 类的代码。
  • 你能发布 MiscUtils
  • 我们不想要 .class。请在您的问题中包含用于定义 MiscUtils 的代码。如果在 MiscClassUtilsTest.java 中定义了 MiscUtils,请编辑您的问题并将代码复制到那里。
  • 我已经添加了 MiscUtils.class 代码
  • Java 区分大小写。如果您有一个名为“calcGrade”的方法,则必须调用“calcGrade”而不是“CalcGrade”。

标签: java compilation compiler-errors


【解决方案1】:

我认为您的 MiscUtils 类缺少编译器错误中显示的方法。

【讨论】:

【解决方案2】:

您尚未导入 MiscUtils 类。因此它给出错误。

在 MiscUtilsTest 类之前使用导入语法。

import MiscUtils.*;

【讨论】:

    【解决方案3】:

    改变:

     int grade1, grade2, grade3, grade4, grade5, grade6, grade7, grade8, grade9;
    

    String grade1, grade2, grade3, grade4, grade5, grade6, grade7, grade8, grade9;
    

    因为calcGrade() 返回的是String 而不是int

    【讨论】:

    • 非常感谢,我在编译时收到了其他几个错误,但我设法修复了它们。感谢大家的帮助,成功编译没有错误。另外,从现在开始,我一定会消除这些错误:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多