我对这个编程领域非常陌生,我正在学习5级课程,现在我正在学习Java.
我在家里试着做一些练习,现在我正在试着建立一个算法,让用户 Select (N)个数字,然后随机显示它们,并计算所有负数的平均值和所有正数的百分比.
但是我的计数器有一个错误,说它不能被0整除,我甚至试着把它初始化为1,在我做数学之前,我从他那里取-1来计算它的原始值,但它不起作用.
会喜欢这里的一些亮点!非常感谢!
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.
}
}