问题
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!
回答1:
You initialize you array. And then default values are given (every int is initialized 0)
int []myArray= new int[num];
int minValue=myArray[0];
it will be 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];
Or use Integer.MIN_VALUE.
回答2:
When you go through to look for either the minimum or maximum of a set of values, it's better to assume that all values will be larger than a default maximum value (i.e. set your maximum value to the smallest possible integer), and that all values will be smaller than a default minimum value (i.e. set your minimum value to be the largest possible integer).
The above sounds counterintuitive, but as you iterate through the array, if you come across a value that is "larger" than the maximum, you update your max value. The same idea applies for the minimum (i.e. if you find a value smaller than your minimum). Since both would start out at their logical extremes, you'll be able to find the true minimum/maximum easier.
The code
int maxValue=myArray[0];
int minValue=myArray[0];
implies that both maxValue and minValue are 0, since a primitive integer array will always populate itself with zeroes. Instead, you should try this:
int maxValue=Integer.MIN_VALUE;
int minValue=Integer.MAX_VALUE;
For some clarification on those Integer constants, check out Integer.MAX_VALUE and Integer.MIN_VALUE in the API.
回答3:
int minValue=myArray[0];
because of this line, your minValue is set to 0. So the minimum value would be reset in this method only if the myArray[i] in the below code is less than 0. Otherwise it remains 0.
for( i=1; i<myArray.length-1;i++)
{
if( myArray[i]<minValue)
{
minValue= myArray[i];
}
}
回答4:
int []myArray= new int[num];
int array default elements to 0
来源:https://stackoverflow.com/questions/10839840/minimum-value-in-java-wont-work