This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2016/3/14

how can i get the smallest and largest number to give the right output

divinity divinity

2016/3/14

#
hi i am practicing a program here. reversed number get smallest and largest numbers get odd and even numbers the reversed numbers is working just the way it is supposed to. the odd and even numbers is working the way it is supposed to work. i am getting the correct output from the both of them it is just the smallest and largest numbers is not giving me the correct output the smallest output is giving is zero the largest output is 3 these are the numbers that i entered: 0,10,12,205,23,100,0,5,11, from that numbers it is supposed to give the reversed numbers, odd and even number and also the smallest and largest. any help would be greatly appreciated on how to get the correct output from the smallest and largest. here is the codes that i used. these are just simple program that i am using as practice.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
int arr[]= new int[10];
       
       int i=0;
       int largest=0,  smallest = 0,num;
       
       for(i=0; i<=9; i++)
       {
          System.out.println("please enter a number");
          arr[i] = userinput.nextInt();
       }
       for(i=9; i>=0; i--)
       {
          System.out.println(arr[i]+"\t");
       }
        
       for(i=1; i<=arr.length; i++)
        {
           if(i==0)
           {
               largest = arr[i];
           }
           else if(arr[i] > largest)
           {
                largest = arr[i];
           }
           else if(arr[i]< smallest)
           {
               smallest = arr[i];
           }
            System.out.println("the smallest number is "+smallest);
            System.out.println("the largest number is "+ largest);
        
        
       for(i=0; i<=9; i++)
       {
           if(arr[i]%2==0)
           {
              System.out.println(arr[i]+" even number");
           }
           else if(arr[i]%2!=0)
           {
              System.out.println(arr[i]+" odd number");
           }
       }
        
danpost danpost

2016/3/15

#
You should probably set the smallest value along with the largest (at line 20) with the first value. Then, it looks like you removed a closing squiggly bracket at line 32 (the 'for' loop starting at line 34 appears to be inside the 'for' block starting at line 16).
divinity divinity

2016/3/15

#
hi danpost i fix it and now i am getting an error. arrayoutofbound exception error and it is not printing the odd and even numbers. can u help
divinity divinity

2016/3/15

#
1
else if(arr[i] < smallest)
this is where it is telling me : at reversednumbers.Reversednumbers.main(Reversednumbers.java:41) and it is repeating itself
danpost danpost

2016/3/15

#
Line 16 should be:
1
for(i=0; i<arr.length; i++)
or, you could make it the same as line 34.
divinity divinity

2016/3/15

#
hi danpost i got it figure, apparently the arr.length was accessing more than the length itself. got the answer in another forum but i have one other problem. i need it to give me the correct smallest and correct largest. these are the numbers that i am using. 0,3,10,12,205,23,100,0,5,11: out of those number it supposed to pick the smallest and the largest, this is what i am getting for the smallest and largest. the smallest: the smallest number is 0; the largest: the largest number is 3; I think I am right with the smallest but I can be corrected. but it is the largest is what i am not too sure about. why is it saying 3 is the largest and not 205
danpost danpost

2016/3/15

#
You need to re-post your code since it has been edited.
divinity divinity

2016/3/15

#
hi danpost here it it
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
int arr[]= new int[10];
        
        int i=0;
        int largest=0,  smallest = 0,num;
        
        for(i=0; i<=9; i++)
        {
           System.out.println("please enter a number");
           arr[i] = userinput.nextInt();
        }
        for(i=9; i>=0; i--)
        {
           System.out.println(arr[i]+"\t");
        }
         
        for(i=1; i<=1; i++)
        {
            if(i==0)
            {
                smallest = arr[i];
            }
            else if(arr[i] < smallest)
            {
                 smallest = arr[i];
            }
            else if(arr[i]> largest)
            {
                largest = arr[i];
            }
             System.out.println("the smallest number is "+smallest);
             System.out.println("the largest number is "+ largest);
         
        }
         
        for(i=1; i<=9; i++)
        {
            if(arr[i]%2==0)
            {
               System.out.println(arr[i]+" even number");
            }
            else if(arr[i]%2!=0)
            {
               System.out.println(arr[i]+" odd number");
            }
        }
danpost danpost

2016/3/15

#
Line 16 seems quite suspicious. It not only starts at the wrong index for the array, but the condition will not allow all the numbers in the array to be checked.
divinity divinity

2016/3/15

#
for(i=1; i<=9; i++) { if(i==0) { smallest = arr; } else if(arr < smallest) { smallest = arr; } else if(arr> largest) { largest = arr; } System.out.println("the smallest number is "+smallest); System.out.println("the largest number is "+ largest); this is the codes and this below is the output which is ah want but not like how it is below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
the smallest number is 0
the largest number is 3
the smallest number is 0
the largest number is 10
the smallest number is 0
the largest number is 12
the smallest number is 0
the largest number is 205
the smallest number is 0
the largest number is 205
the smallest number is 0
the largest number is 205
the smallest number is 0
the largest number is 205
the smallest number is 0
the largest number is 205
the smallest number is 0
the largest number is 205
danpost danpost

2016/3/16

#
You need to close the 'for' block before the output lines. Also, you still have two issues with the code as it is besides that. Your 'for' loop (starting line 16' is not starting at the first element in the array -- but the second element. As such, the first 'if' condition within the loop (at line 18) will never be true (adjust line 16 to fix this issue -- not line 18). The other issue you still have is initially setting 'largest' with the first element along with the 'smallest' one within that 'if' block.
divinity divinity

2016/3/16

#
hi danpost i want to thank you very much. i fix the problem and i see and understand what you were saying just now. thanks again. it giving me the correct output. thanks much. your help and patience is much appreciated
You need to login to post a reply.