gpt4 book ai didi

java - 编译时找不到符号方法?

转载 作者:行者123 更新时间:2023-11-30 08:18:30 25 4
gpt4 key购买 nike

出于某种原因,我的测试类无法识别我的方法类中的方法。帮助?我收到诸如“找不到符号方法 digitToBarCode(int)”和“找不到符号方法 checkDigit(int)”之类的错误

import java.util.Scanner;

public class ZipCode
{
public static int checkDigit(int zip)
{
int remaining = zip;
int sum = 0;
while (remaining > 0) {
sum += remaining % 10;
remaining /= 10;
}
return 10 - (sum % 10);
}

public static String digitToBarCode(int digit) {
if (digit == 1)
{
return ":::||";
}
if (digit == 2)
{
return "::|:|";
}
if (digit == 3)
{
return "::||:";
}
if (digit == 4)
{
return ":|::|";
}
if (digit == 5)
{
return ":|:|:";
}
if (digit == 6)
{
return ":||::";
}
if (digit == 7)
{
return "|:::|";
}
if (digit == 8)
{
return "|::|:";
}
if (digit == 9)
{
return "|:|::";
}
if (digit == 0)
{
return "||:::";
}
return "";

}

}

测试类在这里

import java.util.Scanner;

public class ZipCodeConverter
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a zip code: ");
int zip = input.nextInt();
int checkDigit = checkDigit(zip);
String digitPrint=digitToBarCode(checkDigit);

int specificNum1=(int)((zip / Math.pow(10, 5-1)) % 10);
String swag1=digitToBarCode(specificNum1);


int specificNum2=(int)((zip / Math.pow(10, 4-1)) % 10);
String swag2=digitToBarCode(specificNum2);


int specificNum3=(int)((zip / Math.pow(10, 3-1)) % 10);
String swag3=digitToBarCode(specificNum3);


int specificNum4=(int)((zip / Math.pow(10, 2-1)) % 10);
String swag4=digitToBarCode(specificNum4);


int specificNum5=(int)((zip / Math.pow(10, 1-1)) % 10);
String swag5=digitToBarCode(specificNum5);

String y=swag1+swag2+swag3+swag4+swag5;


System.out.println("|"+y+digitPrint+"|");
}
}

最佳答案

调用另一个类的静态方法时指定类名

int checkDigit = ZipCode.checkDigit(zip);
String digitPrint=ZipCode.digitToBarCode(checkDigit);

找不到符号,因为编译器在 ZipCodeConverter 类中找不到 checkDigit 方法,因为它存在于 ZipCode 类中

例如,在您的代码中,您有一个语句 Math.pow(10, 3-1))Math 是类名,pow() 是其中的一个静态方法。所以你称它为 Math.pow()

关于java - 编译时找不到符号方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27561320/

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