Issue
I'm pretty new to this programming area, I'm taking a level 5 course and I'm learning java now.
I was trying to do some exercises at home, and now I'm trying to build an algorithm that let's the user choose (N) number of numbers, and then randomly display them and make the average of all the negatives, and the percentage of all the positive ones.
But there's this error with my counter, says it can't divide by 0, I even tried to initialize it as 1, and before I do the math I take -1 from him to come to it's original value, but it won't work.
Would appreciate some enlightnment here! Thanks alot!
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Random random = new Random();
Scanner ler = new Scanner(System.in); //ler stands for read
int nPos = 0, nNeg = 0, totalPos = 0, totalNeg = 0, contador = 1;
//contador stands for counter
int N;
double mediaNeg, percPos; //media stands for average
System.out.println("Insira o número de números que pretende inserir: ");
// this print stands for insert the number of numbers you intend to insert
N = ler.nextInt();
for (int i = 0; i == N-1; i++){
int num = random.nextInt(101) - 50;
System.out.println(num);
contador ++;
if (num > 0){
nPos ++;
totalPos += num;
}
else if (num < 0){
nNeg ++;
totalNeg += num;
}
}
contador = contador - 1;
mediaNeg = (double) totalNeg / nNeg;
percPos = (double) (totalPos / contador) * 100;
System.out.println("Média dos negativos = " +mediaNeg);
System.out.print("Percentagem dos positivos = " +percPos);
//These two are supposed to show the average of the negative numbers, and the percentage of the positives.
}
}
Solution
Your code seems mostly correct, but there's a small issue in your for loop condition. The loop should continue while i is less than N, not while i == N-1. The correct loop condition is i < N. Here's the corrected code:
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Random random = new Random();
Scanner ler = new Scanner(System.in);
int nPos = 0, nNeg = 0, totalPos = 0, totalNeg = 0, contador = 1;
int N;
double mediaNeg, percPos;
System.out.println("Insira o número de números que pretende inserir:");
N = ler.nextInt();
for (int i = 0; i < N; i++) { // Corrected loop condition
int num = random.nextInt(101) - 50;
System.out.println(num);
contador++;
if (num > 0) {
nPos++;
totalPos += num;
} else if (num < 0) {
nNeg++;
totalNeg += num;
}
}
contador = contador - 1;
// Check if nNeg is not zero to avoid dividing by zero
if (nNeg != 0) {
mediaNeg = (double) totalNeg / nNeg;
System.out.println("Média dos negativos = " + mediaNeg);
} else {
System.out.println("Não há números negativos para calcular a média.");
}
// Calculate percentage only if there are positive numbers
if (contador != 0) {
percPos = (double) (totalPos / contador) * 100;
System.out.print("Percentagem dos positivos = " + percPos);
} else {
System.out.println("Não há números positivos para calcular a percentagem.");
}
}
}
These changes should help you avoid the divide-by-zero error and provide more accurate results. I also added some checks to handle cases where there are no negative or positive numbers entered by the user.
Answered By - Owais Yosuf
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.