目前编写简单程序。我希望在52周内显示每次访问商店的百分比。例如,"在52周内,商店1号被检查了20%"。 这是迄今为止的代码。
public class StoreSelection {
public static void main(String[] args) {
for( int i=1; i<=52;i++){
int randomSelection = 1+(int)(Math.random()*4);
System.out.println(i+" week"+" Store number "+randomSelection+" will be inspected");
}
// here should be stated "In 52 weeks store number 1 was visited (number of percentage)
// and then below "In 52 weeks store number 2 was visited (number of percentage)
//and same for number 3 and 4.
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
公共类StoreSelection {
public static void main(String[] args) {
int weeks = 52;
int storeCount = 4;
int[] stores = new int[storeCount];
for (int i = 0; i < weeks; i++) {
int randomSelection = 1 + (int) (Math.random() * storeCount);
System.out.println((i+1) + " week: " + " Store number " + randomSelection
+ " will be inspected");
stores[randomSelection-1]++;
}
for (int i = 0; i < storeCount; i++) {
System.out.println("Store " + (i+1) + " was visited "
+ (stores[i] / 52.0 * 100.0) + "% times");
}
}
}