anyone know how to fix this ?
hello guys , can anyone tell me what should i do to change the output number from 193 to 193.5 for the average number . the last number always keep missing when i get the output . should i add any other code ? thanx for your cooperation . i also attach the code and output for from what i have done .
p/s : sorry for my bad english . :eek:
images removed by moderator because they linked to a bad site -KevinWorkman
Re: anyone know how to fix this ?
The images you originally supplied link to a site that contains pornographic advertisements. That's not great, as some of us are at work, and some of us have wives and stuff.
If you want to post code, post the text, not an image. Use the code tags and post an SSCCE. Don't post screenshots of your code. Same goes with error messages or stack traces.
Re: anyone know how to fix this ?
owh sorry . i didnt know that . newbie here . okay then , i will post the code . thanks for your advice .
so this is the coding for what i have done . still cant get what i want .
Code java:
import java.util.*;
class lab1{
public static void main(String[] args){
int sum=0,count=0;
Scanner input=new Scanner(System.in);
System.out.println("Enter a Number: ");
int array[]=new int[10];
for(int i=0;i<array.length;i++){
int num=input.nextInt();
array[i]=num;
}
int maxValue = array[0];
for(int i=1;i < array.length;i++){
if(array[i] > maxValue){
maxValue = array[i];
}
}
for(int i=0;i<array.length;i++){
sum+=array[i];
if(array[i]==maxValue){
count++;
}
}
System.out.println("Total Of Numbers is: "+sum);
System.out.println("Average of Numbers is: "+sum/array.length);
System.out.println("Largest No: "+maxValue+" which occurs "+count+" times.");
Arrays.sort(array);
System.out.println("Numbers in ascending order: ");
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
System.out.println("Numbers in descending order: ");
for(int i=array.length-1;i>=0;i--){
System.out.println(array[i]);
}
}
}
Re: anyone know how to fix this ?
When posting code, make sure you use the highlight tags. I've added them for you this time.
But your whole problem is that you're using ints, which are only whole numbers (0, 1, 2, not 1.5 or 2.3). That's fine for things that are only whole numbers (counts of things, indexes, etc), but if you want to have decimals (for things like averages), you have to use double or float.
Re: anyone know how to fix this ?
okay , next time i will use the highlight tags , thanx once again for your advice . im really humble here . -__-" let me try using double or float , hope it works .
Re: anyone know how to fix this ?
Note that you do not have to convert all your variables to double if it doesn't make sense. All you need to do is cast your sum to a double when doing the calculation.
Code java:
System.out.println("Average of Numbers is: " + (double)sum/array.length);
Re: anyone know how to fix this ?
hey junky , it works man . thank u so much . :o
Re: anyone know how to fix this ?
If you click 'Go Advanced' you can attach images directly to your posts.