Accp7.0第12章课后题第5题参考答案

来源:互联网 发布:人力资源大数据分析 编辑:程序博客网 时间:2024/04/28 21:25
import java.util.Scanner;public class zuoye2 {String name;int money;int monry1;int a;public void a() {int i = (int) (Math.random() * 3 + 1);switch (i) {case 1:name = "公主电动车";monry1 = 1000;break;case 2:name = "玩偶";monry1 = 400;break;case 3:name = "玩具车";monry1 = 500;break;}System.out.println("商品为:" + name);System.out.println("请猜测" + name + "的价格:" + monry1);Scanner input = new Scanner(System.in);for (a = 1; a <= 4; a++) {System.out.println("再猜一次:");money = input.nextInt();if (a <= 3) {                     if (money < monry1) {System.out.println("再大点!");} else if (money > monry1) {System.out.println("再小点");} else {System.out.println("猜对了");break;}}if (a == 4) {if (money == monry1) {System.out.println("猜对了");break;} else {System.out.println("4次全错,下次努力");}}}}}
public class Text2 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubzuoye2 a = new zuoye2();a.a();}}


2 0
原创粉丝点击