一个简单的java题目,但是报错我无法解决 报错提示里面有UTF-8 -classpath ,当我把整个while循环注释掉不会报错
题目描述
一个大V直播抽奖,奖品是现金红包,分别有(2,588,888,100e,1000五个奖金。请使用代码模拟抽奖,打印出每个奖项,奖项的出现顺序要随机且不重复。
打印效果如下:(随机顺序,不一定是下面的顺序)
888元的奖金被抽出
588元的奖金被抽出
18080元的奖金被抽出
1000元的奖金被抽出
2元的奖金被抽出
报错:/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=49288:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/xiaozheng/IdeaProjects/project/out/production/one two.redPacket
代码部分
package two;
import java.util.Random;
public class redPacket {
public static void main(String[] args){
int []a={2,588,888,1000,10000};
int []b=new int[5];
int count=0;
while(count!=5){
Random r=new Random();
int j=r.nextInt(5);
for (int i = 0; i <count; i++) {
if(b[i]==a[j]){
break;
}
b[count]=a[j];
count++;
}
}
for (int i = 0; i < b.length; i++) {
System.out.println(b[i]);
}
}
}
关于#java#的问题,如何解决?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
关注
帮你重写了一个,如下:
public static void main(String[] args){ int [] a={2,588,888,1000,10000}; Random random = new Random(); Set<Integer> set = new HashSet<>(); int count=0; while(count<5) { int temp = random.nextInt(5); if (!set.contains(temp)) { System.out.println(a[temp]+"元的奖金被抽出"); set.add(temp); count++; } } }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报