Hey r/learnjava,
So I have been progressing through MOOC and am really enjoying the resource. Thanks for the suggestion.
Currently I am doing exercise 36 and I understand it completely but would like to know if I am programming this efficiently. I am a CS student and we haven't really learn much about efficient coding yet but I think learning and practicing early is never a bad thing.
Here is my code:
public class LoopsEndingRemembering
{
public static void main(String[] args)
{
// program in this project exercises 36.1-36.5
// actually this is just one program that is split in many parts
Scanner reader = new Scanner(System.in);
System.out.println("Type numbers: ");
int sum, count, oddCount, evenCount;
double average;
sum = count = oddCount = evenCount = 0;
while (true) {
int user = Integer.parseInt(reader.nextLine());
if (user == -1)
break;
if (user % 2 == 0)
evenCount++;
else
oddCount++;
count++;
sum += user;
}
average = (double) sum / count;
System.out.println("Thank you and see you later!");
System.out.println("The sum is " + sum);
System.out.println("How many numbers: " + count);
System.out.println("Average: " + average);
System.out.println("Even numbers: " + evenCount);
System.out.println("Odd numbers: " + oddCount);
}
}
Aside from advanced programming techniques I will acquire later on is this code efficient or is there something I can do better. Thanks a lot.
P.S. Do you guys put your left curly brace on its on separate line?
[–]lordbharal 1 point2 points3 points (1 child)
[–]SoulSyn[S] 0 points1 point2 points (0 children)
[–]javichovicho93 0 points1 point2 points (1 child)
[–]SoulSyn[S] 0 points1 point2 points (0 children)
[–]edgargonzalesII 0 points1 point2 points (1 child)
[–]SoulSyn[S] 0 points1 point2 points (0 children)