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

2016/1/7

tell me what I am not seeing here

divinity divinity

2016/1/7

#
hi i am doing this array program where the user has to prompt for 5 numbers and to store them in an array. the program will then print out the 5 numbers entered and the smallest one entered. here is my codes and the output. tell me why i am not getting the required output the output is not what is supposed to output. any hl\elp and understanding would be appreciated.
 int value[]= {1,2,3, 4,5};
        
        int i = 0;
        int smallest = 1;
        
        
        for(i=0; i<5; i++)
        {
            System.out.println("enter a number");
            value[i] = userinput.nextInt();
           
        if(i == 1)
        {
           smallest = value[i];
        }
         if(value[i] < smallest)
        {
           smallest = value[i];
        }
           System.out.println("the smallest value is "+ value[i]);
           
        }
         here is the output
enter a number
1
the smallest value is 1
enter a number
2
the smallest value is 2
enter a number
3
the smallest value is 3
enter a number
4
the smallest value is 4
enter a number
5
the smallest value is 5
BUILD SUCCESSFUL (total time: 9 seconds)
davmac davmac

2016/1/7

#
System.out.println("the smallest value is "+ value[i]);
You're printing the number that just entered, instead of the smallest number.
divinity divinity

2016/1/7

#
what do i do to get it to print the smallest number because the system.out.println(the smallest values is'+ value) supposed to output the smallest number from the 5 numbers that is stored in the array
davmac davmac

2016/1/7

#
what do i do to get it to print the smallest number
Tell it to print the smallest number.
System.out.println("the smallest value is "+ smallest);
divinity divinity

2016/1/8

#
i am getting different result since i did what you say still not getting the said result. here is the latest result i am getting.
enter a number
1
 the smallest value is 0
enter a number
2
 the smallest value is 2
enter a number
3
 the smallest value is 2
enter a number
4
 the smallest value is 2
enter a number
5
 the smallest value is 2
BUILD SUCCESSFUL (total time: 7 seconds)
Super_Hippo Super_Hippo

2016/1/8

#
Change '1' in line 12 to '0'.
divinity divinity

2016/1/8

#
how am i supposed to do that. that is the output not the actual codes. this is the codes.
   int value[]= {1,2,3, 4,5};
        
        int i;
        int smallest = 0;
        
        
        for(i=0; i<5; i++)
        {
            System.out.println("enter a number");
            value[i] = userinput.nextInt();
           
        if(i == 0)
        {
           smallest = value[i];
        }
         if(value[i] < smallest)
        {
           smallest = value[i];
        }
           System.out.println(" the smallest value is "+ smallest);
        }
         
        
danpost danpost

2016/1/8

#
divinity wrote...
how am i supposed to do that. that is the output not the actual codes. this is the codes. < Code Omitted >
The code you just posted looks okay. Does it not work satisfactorily?
divinity divinity

2016/1/8

#
no danpost okay danpost i think i have got it now. ah now see it thanks danpost and davmac very much
You need to login to post a reply.