import java.util.ArrayList; import java.util.List; import java.util.Random; public class Test1{ public static voidmain(String[] args){ List awardList = new ArrayList(); Award award1 = new Award(); award1.setAwardName("手机");//10% award1.setProbability(0.1); awardList.add(award1); Award award2 = new Award(); award2.setAwardName("水杯");//20% award2.setPorbability(0.2+0.1);//概率加上上一个奖品的概率(累加) awardList.add(award2); Award award4 = new Award(); award4.setAwardName("鼠标");//10% award4.setPorbability(0.1+0.2+0.1); awardList.add(award4); Award award5 = new Award(); award5.setAwardName("谢谢惠顾");//60% award5.setPorbability(1); awardList.add(award5); Random random = new Random(); int a = 0,b = 0,c = 0,d = 0; for (int i = 0; i < 10000; ) { Double userSelect = random.nextDouble();//0.0-1.0的double随机数(不包括1.0) String awardName = duageAward(userSelect, awardList); if ("手机".equals(awardName)) { a++; } else if ("水杯".equals(awardName)) { b++; } else if ("鼠标".equals(awardName)) { c++; } else if ("谢谢惠顾".equals(awardName)) { d++; } } System.out.println("手机"+ a +"次"); System.out.println("水杯"+ b +"次"); System.out.println("鼠标"+ c +"次"); System.out.println("谢谢惠顾"+ d +"次"); } } 这个代码的错误在哪
时间: 2024-01-13 21:04:22 浏览: 79
这段代码存在一些语法错误,具体来说:
1. `public static voidmain(String[] args)` 应该改为 `public static void main(String[] args)`,缺少了空格。
2. `award2.setPorbability(0.2+0.1)` 应该改为 `award2.setProbability(0.2+0.1)`,单词拼写错误。
3. `award4.setPorbability(0.1+0.2+0.1)` 应该改为 `award4.setProbability(0.1+0.2+0.1)`,单词拼写错误。
4. `for (int i = 0; i < 10000;)` 应该改为 `for (int i = 0; i < 10000; i++)`,缺少了循环变量的自增语句。
因此,修正后的代码应该是:
```
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Test1 {
public static void main(String[] args) {
List<Award> awardList = new ArrayList<>();
Award award1 = new Award();
award1.setAwardName("手机");//10%
award1.setProbability(0.1);
awardList.add(award1);
Award award2 = new Award();
award2.setAwardName("水杯");//20%
award2.setProbability(0.2 + 0.1);//概率加上上一个奖品的概率(累加)
awardList.add(award2);
Award award4 = new Award();
award4.setAwardName("鼠标");//10%
award4.setProbability(0.1 + 0.2 + 0.1);
awardList.add(award4);
Award award5 = new Award();
award5.setAwardName("谢谢惠顾");//60%
award5.setProbability(1);
awardList.add(award5);
Random random = new Random();
int a = 0, b = 0, c = 0, d = 0;
for (int i = 0; i < 10000; i++) {
Double userSelect = random.nextDouble();//0.0-1.0的double随机数(不包括1.0)
String awardName = duageAward(userSelect, awardList);
if ("手机".equals(awardName)) {
a++;
} else if ("水杯".equals(awardName)) {
b++;
} else if ("鼠标".equals(awardName)) {
c++;
} else if ("谢谢惠顾".equals(awardName)) {
d++;
}
}
System.out.println("手机" + a + "次");
System.out.println("水杯" + b + "次");
System.out.println("鼠标" + c + "次");
System.out.println("谢谢惠顾" + d + "次");
}
private static String duageAward(Double userSelect, List<Award> awardList) {
Double proSum = 0d;//概率累加
for (Award award : awardList) {
proSum += award.getProbability();//累加当前奖品概率
if (userSelect < proSum) {//如果随机数小于当前累加的概率之和,说明抽中当前奖品
return award.getAwardName();//返回当前奖品名称
}
}
return null;//未抽中任何奖品
}
}
class Award {
private String awardName;//奖品名称
private Double probability;//中奖概率
public String getAwardName() {
return awardName;
}
public void setAwardName(String awardName) {
this.awardName = awardName;
}
public Double getProbability() {
return probability;
}
public void setProbability(Double probability) {
this.probability = probability;
}
}
```
注意,修正后的代码中还添加了一个 `Award` 类,用于封装奖品信息。
阅读全文