error: incompatible types: String cannot be converted to int in Java
本问题已经有最佳答案,请猛点这里访问。
我试图从cmd中获取参数,并将该参数传递给fibonacci方法,但我得到了一些错误。有人能告诉我为什么会出现这个错误吗?
谢谢您。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | class fibo { static void Fibonacci(int Total,int n1,int n2){ if(Total > 1){ int sum = n1 + n2; System.out.print(sum +""); n1 = n2; n2 = sum; --Total; Fibonacci(Total,n1,n2); } } } class main{ public static void main(String args[]){ fibo f = new fibo(); System.out.print(0 +""); System.out.print(1 +""); int Total = args[0]; int n1 = args[1]; int n2 = args[2]; f.Fibonacci(Total,n1,n2); } } |
错误:
1 2 3 4 5 6 7 8 9 10 11 12 | fibonacci.java:26: error: incompatible types: String cannot be converted to int int Total = args[0]; fibonacci.java:27: error: incompatible types: String cannot be converted to int int n1 = args[1]; fibonacci.java:28: error: incompatible types: String cannot be converted to int int n2 = args[2]; 3 errors |
1 2 | int n1 = args[1]; int n2 = args[2]; |
您试图将
对于EX:
1 |
其他作业也一样。