你好,我的代码可能不是最直观的,但是Java是一门面向对象的语言,应该用面向对象的思想写,这点很重要。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException{
Product[] p = { new Product(1,"T恤",245),
new Product(2,"网球鞋",570),
new Product(3,"网球拍",320)
};
System.out.println("1.T恤\t2.网球鞋\t3.网球拍\n................................");
while(true){
System.out.print("请输入商品编号:");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int i = Integer.parseInt(input.readLine());
if(i==1||i==2||i==3)
System.out.println(p[i-1].name + "\t¥" + p[i-1].price);
System.out.print("是否继续(y/n)");
String s = input.readLine();
if(s.equals("n")){
System.out.println("程序结束");
break;
}
}
}
}
class Product{
int num;
String name;
float price;
Product(int num, String name, float price){
this.num = num;
this.name = name;
this.price = price;
}
}
运行结果:
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/0df431adcbef76094ec6c7e42adda3cc7dd99eff?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
希望可以帮到你!