用JAVA语言写随即猜测商品价格!

发布网友 发布时间:2022-04-22 00:23

我来回答

1个回答

热心网友 时间:2023-07-12 00:36

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class Sort {
public static void main(String args[]) {
Random rand = new Random();
String shop[][]={{"牙膏","7"},{"牙刷","6"},{"飞机","10"}};

int i=(int) (Math.random() * (shop.length));

int price=Integer.parseInt(shop[i][1]);
System.out.println("随即商品:"+shop[i][0]);

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请竞猜商品价格:(限20次)");
int a=0;
int count=1;
for(int j=0;j<20;j++)
{
count++;
try {
a= Integer.parseInt(br.readLine());
if(count<=20){

if(a==price){
System.out.println("恭喜你!!价格正确!!");
break;
}
else{
System.out.println("很遗憾,价格错误");
}
}
else{
System.out.println("20次机会用完");
}
}

catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}

我的测试:
随即商品:飞机
请竞猜商品价格:(限20次)
1
很遗憾,价格错误
2
很遗憾,价格错误
3
很遗憾,价格错误
10
恭喜你!!价格正确!!

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com