gpt4 book ai didi

java - 当我创建方法时,我的 Java 代码显示编译器错误

转载 作者:行者123 更新时间:2023-12-01 20:26:30 25 4
gpt4 key购买 nike

package aj;

import java.util.Scanner;

public class ConvertingNumber {

public static void main(String[] args) {

Scanner a = new Scanner(System.in);
System.out.println("Enter the Number");
int num = a.nextInt();
System.out.println("Enter the base for the given number");
int base = a.nextInt();
converting(num,base);

public static int converting(int num , int base) {

String sum="";

while(num > 0) {
int rem = 0;
rem = num % base;
num = num / base;
sum = rem + sum;
}

System.out.println(sum);
}
}
}

对于我上面的java代码,我收到编译器错误说:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The method converting(int, int) is undefined for the type ConvertingNumber
void is an invalid type for the variable converting
Syntax error on token "(", ; expected
Duplicate local variable num
Syntax error on token ",", ; expected
Duplicate local variable base
Syntax error on token ")", ; expected
at aj.ConvertingNumber.main(ConvertingNumber.java:12)

请任何人帮我解决这个问题。提前致谢。

最佳答案

转换方法应该位于主方法之外

package aj;

import java.util.Scanner;

public class ConvertingNumber {
public static void main(String[] args) {
Scanner a=new Scanner(System.in);
System.out.println("Enter the Number");
int num=a.nextInt();
System.out.println("enter the base for the given number");
int base=a.nextInt();
converting(num,base);
}

//This method should be out side the main method
public static void converting(int num , int base) {
String sum="";
while(num>0) {
int rem=0;
rem=num%base;
num=num/base;
sum=rem+sum;
}
System.out.println(sum);

}

}

关于java - 当我创建方法时,我的 Java 代码显示编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43823419/

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