Java中的最小值将不起作用 [英] minimum value in java won't work

查看:45
本文介绍了Java中的最小值将不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,因为我的脑细胞无法找到此程序的问题! 这是代码

I need help because my brain cells cannot find what is wrong with this program! Here's the code

     import java.util.*;
      public class student{
      public static void main (String []args)

    { 
        Scanner sc=new Scanner(System.in);
        System.out.println("enter number elements");  //asking the user to enter the number of integer items 
        int num=sc.nextInt();
        int []myArray= new int[num];
        int maxValue=myArray[0];
        int minValue=myArray[0];
        int i;
        for( i=0; i<myArray.length;i++)
        {System.out.print("Enter element"+(i+1));  //asking the user to enter the items 
           myArray[i]=sc.nextInt();
        }

        for(i=0; i<myArray.length;i++)
        {System.out.print(myArray[i]);    //displaying the elements 
        }
        System.out.println(" ");
        for( i=myArray.length-1; i>=0;i--)
        {System.out.print(myArray[i]);      //displaying the elements in a backward order
        }
        System.out.println(" ");
        for( i=0; i<myArray.length;i++)
        {if(i%2==1)

           System.out.println(myArray[i]);               //displaying the elements in odd indices

        }

      for( i=0; i<myArray.length-1;i++)
        { if(myArray[i]>maxValue)
            {
                maxValue= myArray[i];           //finding the maximum


            }

        }
        System.out.println(maxValue+" "+(i+1));

        for( i=1; i<myArray.length-1;i++)
        { if( myArray[i]<minValue)
            {minValue= myArray[i];                //finding the minimum

            }
        }
       System.out.println(+minValue+" "+(i+1));



        System.out.println(myArray[0]);                          //displaying first item
        System.out.println( myArray[myArray.length-1]);          //displaying last item




        for( i=0; i<myArray.length;i++)
        {if(i%2==0)

          {  myArray[i]= myArray[i]*-1;                    //multiplying items in even indices by -1

        System.out.print( myArray[i]);}
    }
  }

}

程序执行注释中的操作,但是即使我不输入最小值,最小值也始终为零...我无法找出问题所在,因此,感谢您的帮助!

The program does what's in the comment, yet the minimum value is always zero even if I don't enter it...I cannot figure out what's the problem, so I'd appreciate your help!

推荐答案

您初始化数组.然后给出默认值(每个int都初始化为0)

You initialize you array. And then default values are given (every int is initialized 0)

   int []myArray= new int[num];
   int minValue=myArray[0];

它将为0

因此,如果您输入正整数,则找不到小于零的数字

so nothing smaller can be found than zero if you type in positive integers

解决方案 首先用用户输入填充数组 然后做

Solution First fill your array with the user input THEN do

 int minValue=myArray[0];

或使用Integer.MIN_VALUE.

Or use Integer.MIN_VALUE.

这篇关于Java中的最小值将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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