Question:
java problem- why does this code work, and it is right?
letal
2012-05-18 01:38:57 UTC
Have a problem with my Java code. Sitting and trying to write a code that finds the next largest number in a field where the user must enter 10 integers and the program will then find the second largest. By chance, when I sit and try some different ways to work this code (?)

public class nastStorsta {

/ **
* @ Param args
* /
public static void main (String [] args) {
/ / TODO Auto-generated method stub
int number [] = {1,2,3,4,5,6,7,8,9,10};
int max = Integer.MIN_VALUE;
max2 = int Integer.MIN_VALUE;
for (int i = 0; i max2 = Math.max (max, i);
}
System.out.println (max2);


}

}

Why does this happen?
Three answers:
AnalProgrammer
2012-05-18 02:41:21 UTC
Try this

public static void main(String[] args) {

// TODO code application logic here

int number[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int max = number[0];

int max2 = number[0];

for (int i = 1; i < number.length; i++) {

if (number[i] > max) {

if (max > max2) {

max2 = max;

}

max = number[i];

} else if (number[i] > max2 && number[i] < max || max == max2) {

max2 = number[i];

} //End if

}

System.out.println(max2);

}



Your code

max2 = Math.max (max, i);

is completely wrong and should be

max2 = Math.max (max, number[i]);



Tested with

int number[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

and

int number[] = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10};





Have fun.
kleckner
2016-12-03 05:56:20 UTC
You initialized the array purchase, yet you by no potential assigned aPurchase gadgets as any of its products. You left it with no longer something in it. So whilst Java first seems at that first array index, it unearths a null pointer interior the array, makes a decision (properly) that there is no merchandise being pointed to, and throws an errors. it truly is exactly what you could anticipate. with a view to sidestep the errors, you could desire to the two positioned some gadgets interior the array, or carry out an specific verify for null on each and each technology. it must be which you assumed array announcement could artwork an identical way in Java because it does in C/C++. In C/C++, your code could artwork, by way of fact an array is genuinely a sequence of gadgets of that style in memory. Java is in comparison to this. An array of aPurchase gadgets in Java isn't genuinely a sequence of aPurchase gadgets, yet particularly a sequence of preparation to aPurchase gadgets. those rules initiate out null, i.e. no longer pointing to something. additionally, ten million gadgets is one hell of a crapload of gadgets. Even without genuinely initializing any of the contents of that array, it is nonetheless going to soak up 40 megabytes of memory in basic terms storing the guidelines. once you notice your self initializing an array to 3 vast quantity like that, you could probable ask your self despite if a dynamically sized documents shape which contain an ArrayList won't artwork greater powerful.
James Bond
2012-05-18 04:26:48 UTC
int number [] = {1,2,3,4,5,6,7,8,9,10};

int max, max2;

if( number[0]
else

{

max=number[0]; max2=number[1]; }



for (int i = 2; i
if(number[i]>max) { max2=max; max=number[i]; }

else if( max2
}





System.out.println (max2);


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...