求大神帮忙把Java代码写出来

如题所述

public static void main(String[] args) {
String[] name={"T恤","网球鞋","网球拍"};
String[] money={"¥245”,"¥570“,“¥320”};
System.out.println("*******************************");
System.out.println("请选择购买的商品编号:\n");
System.out.println("1.T恤 2.网球鞋 3.网球拍");
System.out.println("*******************************\n");
Scanner input=new Scanner(System.in);
Boolean type=true;
while (type) {
System.out.println("请输入商品编号");
int no=input.nextInt();
System.out.println(name[no]+"\t"+money[no]+"\n");
System.out.println("是否继续?(y/n)");
if (input.next().equals("n")) {
type=false;
}
}
System.out.println("程序结束!");
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-03-05

你好,我的代码可能不是最直观的,但是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;
}
}

运行结果:

希望可以帮到你!

第2个回答  2015-03-05
import java.util.Scanner;

public class Price {
public static void main(String[] args) {
int[] prices = {245, 570, 320};
Scanner scanner = new Scanner(System.in);
String line;
System.out.print("type a number or n:");
while((line = scanner.nextLine()) != "n") {
int no = Integer.parseInt(line);
System.out.println("price is " + prices[no - 1]);
System.out.print("type a number or n:");
}
System.out.println("exit normally!");
}
}
相似回答