烦请老师指正和提出改进意见~

烦请老师指正和提出改进意见~

老师我写了两种,但是不知道哪个更符合作业要求,我猜测作业应该有个隐含条件:每人三次抽奖机会,但是一旦抽中了,剩余的抽奖机会就作废了。

第一种代码:

package imooc_redo_exercise;

import java.util.Scanner;

public class Exerccise8_2 {

public static void main(String[] args) {
System.out.println("Please enter the customer's total amount of spending:");
Scanner sc = new Scanner(System.in);
float sum = sc.nextFloat();
if(sum >= 100.0) {
System.out.println("Congratulations! You got the chance to win a prize!");
for(int i = 1; i <= 3; i++) {
int num = (int) (Math.random()*3 + 1);
System.out.println("num=" + num);
if (num == 1) {
System.out.println("Congratulations! You got a keyboard!");break;
}else if(num ==2) {
System.out.println("Congratulations! You got a kettle!");break;
}else if(num ==3) {
System.out.println("Congratulations! You got a souvenir!");break;
}else {
System.out.println("You got nothing! Wish you good luck next time");
}
}
}else {
System.out.println("Sorry! You are unqualified to participate in the activity!");

}

}

}

第二种代码:

​package imooc_redo_exercise;

import java.util.Scanner;

public class Exercise8_2_2 {

public static void main(String[] args) {
System.out.println("Please enter the customer's total amount of spending:");
Scanner sc = new Scanner(System.in);
float sum = sc.nextFloat();
int i = 1;
int num;
if (sum >= 100.0) {
System.out.println("Congratulations! You got the chance to win a prize!");

do {
num = (int) (Math.random() * 3 + 1);
System.out.println("num=" + num);

switch (num) {
case 1:
System.out.println("Congratulations! You got a keyboard!");

break;
case 2:
System.out.println("Congratulations! You got a kettle!");

break;
case 3:
System.out.println("Congratulations! You got a souvenir!");

break;
default:
System.out.println("Congratulations! You got a souvenir!");

break;
}
i++;
} while ((i <= 3) && (num == 0));
} else {
System.out.println("Sorry! You are unqualified to participate in the activity!");

}

}

}


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学你好,作业要求,当充值会员满百元,每人都有三次抽奖机会。但是同学的代码中有点问题:


在第一种方法中,if后有break,执行if后会跳出循环,不能执行三次。

http://img1.sycdn.imooc.com//climg/612c94ee09b31d2e06490366.jpg

另外(int)(Math.random()*3)+1的结果是1,2,3三个整数,永远不会执行else中的内容。

修改意见:建议去掉break;另外建议将(int)(Math.random()*3)+1改为(int)(Math.random()*4),即随机出现0,1,2,3四个数。


第二种方法中,num=(int)(Math.random()*3)+1的结果是1,2,3三个整数,num永远不为0,所以不满足do-while()中的循环条件,导致执行一次就不再执行。

修改意见:建议将int)(Math.random()*3)+1改为(int)(Math.random()*4),即随机出现0,1,2,3四个数。另外do-while()循环条件中,去掉num==0的判断。

祝学习愉快~

  • Heijyu 提问者 #1

    老师,第一种方法去掉break以后,输出的结果如下图,不大对劲啊

    http://img1.sycdn.imooc.com//climg/612d997f097fd9aa15881494.jpg

    2021-08-31 10:53:35
  • Heijyu 提问者 #2

    我知道了,这是三次抽奖的结果,我老想着只能中一次


    2021-08-31 10:57:10
  • 好帮手慕阿满 回复 提问者 Heijyu #3

    同学理解就好。充值满百元后,每个人都有三次抽奖机会, 不论是否抽中,都可以抽三次。

    祝学习愉快~

    2021-08-31 13:07:14
残泪 2021-08-30 15:14:51

三次抽奖机会不代表只能中一回然后就不能用其他的了呀还是要抽三次

用for循环三次就可以实现抽三次奖!


问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师