Java:方法main中已经定义了变量 [英] Java: Variable is already defined in method main

查看:129
本文介绍了Java:方法main中已经定义了变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,每次尝试编译.java文件时,我都会遇到以下错误

So I have been the following error every time I try to compile my .java file

错误:方法max(String [])中已经定义了变量maxint max = j;"

"error: variable max is already defined in method main(String[]) int max = j; "

我还无法弄清楚问题是什么或如何解决.现在被卡住了大约2个小时.我最终想要做的是在数组中输入一个整数,然后按从最小到最大的顺序对整数进行排序,以提供上下文.

And I haven't been able to figure out what is the issue or how to fix it. Been stuck at it for about 2 hours now. What I'm trying to ultimately do is enter one integer into my array and sorting the digits in that integer from least to greatest to provide context.

这是我代码中的相关部分:

Here is the relevant part from my code:

   int[] wholeNumber = new int[1];


   //Sorting algorithm beginning
   int n = wholeNumber.length;

   System.out.println("Length of array is :" + n); //Array length displayed

   for(int i = 0; i < 1; i++)
   {
     System.out.println("Hello!");

     int max = i;

     for(int j = i+1; j < 1; j++)
     {
       if (wholeNumber[j] > wholeNumber[max])
       {  
          int max = j;
       }

     }
     if (max != i)
     {
        wholeNumber[i] = wholeNumber[max];
        wholeNumber[max] = wholeNumber[i];
     }



   }
   //Sorting algorithm end

推荐答案

如错误所示,您两次声明了 int max .您需要将第二个变量更改为变量赋值,而不是声明:

As the error says, you have int max declared twice. You need to change the second one to a variable assignment, not declaration:

 int max = i;

 for(int j = i+1; j < 1; j++)
 {
   if (wholeNumber[j] > wholeNumber[max])
   {  
      max = j; // NOT: int max = j;
   }
 }

这篇关于Java:方法main中已经定义了变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆