大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.util.ArrayList;
成都创新互联专注于福山企业网站建设,响应式网站开发,成都商城网站开发。福山网站建设公司,为福山等地区提供建站服务。全流程按需开发网站,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务
import java.util.Arrays;
import java.util.List;
public class Recipe {
private String name; //菜谱名称
private String style; //菜系 如:川菜、湘菜等
private int time; //烹饪时长 分钟
private String[] food; //食材
private String[] step; //操作步骤
public Recipe() {
}
public Recipe(String name, String style, int time, String[] food, String[] step) {
this.name = name;
this.style = style;
this.time = time;
this.food = food;
this.step = step;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public String[] getFood() {
return food;
}
public void setFood(String[] food) {
this.food = food;
}
public String[] getStep() {
return step;
}
public void setStep(String[] step) {
this.step = step;
}
@Override
public String toString() {
String foods="";
for (String f : food) {
foods+=f+" ";
}
String steps="";
for (int i = 0; i step.length; i++) {
steps += (i+1)+"."+step[i];
if(i!=step.length-1){
steps+=";";
}
}
return "菜谱名称:" + name +
"\n菜系:" + style +
"\n时长:" + time +
"\n所需食材:" + foods +
"\n操作步骤:" + steps;
}
public static void print(Recipe[] recipes){
for (Recipe recipe : recipes) {
System.out.println(recipe);
}
}
public static Recipe[] searchRecipesContainName(Recipe[] recipes, String name){
ListRecipe list=new ArrayListRecipe();
for (Recipe recipe : recipes) {
if(recipe.getName().contains(name)){
list.add(recipe);
}
}
return list.toArray(new Recipe[list.size()]);
}
public static Recipe[] searchRecipes(Recipe[] recipes, String style){
ListRecipe list=new ArrayListRecipe();
for (Recipe recipe : recipes) {
if(recipe.getStyle().equals(style)){
list.add(recipe);
}
}
return list.toArray(new Recipe[list.size()]);
}
public static Recipe[] searchRecipeLessThan(Recipe[] recipes, int time){
ListRecipe list=new ArrayListRecipe();
for (Recipe recipe : recipes) {
if(recipe.getTime()time){
list.add(recipe);
}
}
return list.toArray(new Recipe[list.size()]);
}
public static Recipe[] searchRecipeContainsFood(Recipe[] recipes, String food){
ListRecipe list=new ArrayListRecipe();
for (Recipe recipe : recipes) {
for (String s : recipe.getFood()) {
if(s.equals(food)){
list.add(recipe);
}
}
}
return list.toArray(new Recipe[list.size()]);
}
public static void main(String[] args) {
//存储5个菜谱的信息
Recipe[] recipes=new Recipe[5];
recipes[0]=new Recipe("酱牛肉","家常菜",120,
new String[]{
"牛腱子",
"黄豆酱油",
"黄酒",
"冰糖"
},
new String[]{
"准备好主要食材",
"加入食材慢炖两至三小时"
});
recipes[1]=new Recipe("红烧牛肉","家常菜",120,
new String[]{
"牛腩",
"牛筋",
"生抽",
"冰糖"
},new String[]{
"准备好主要食材",
"加入食材慢炖两至三小时"
});
recipes[2]=new Recipe("小鸡炖蘑菇","湘菜",100,
new String[]{
"小鸡",
"蘑菇"
},
new String[]{
"准备好主要食材",
"加入食材慢炖两至三小时"
});
recipes[3]=new Recipe("地三鲜","川菜",25,
new String[]{
"茄子",
"辣椒"
},
new String[]{
"准备好主要食材",
"加入食材慢炖"
});
recipes[4]=new Recipe("西红柿","湘菜",20,
new String[]{
"西红柿",
"味达美酱油"
},
new String[]{
"准备好主要食材",
"加入食材慢炖"
});
System.out.println("找到所有名称中包含牛肉的菜谱:");
Recipe[] result_1 = searchRecipesContainName(recipes, "牛肉");
print(result_1);
System.out.println("\n希望查找所有湘菜的菜谱:");
Recipe[] result_2=searchRecipes(recipes,"湘菜");
print(result_2);
System.out.println("\n查找烹饪时长小于30分钟的菜谱:");
Recipe[] result_3=searchRecipeLessThan(recipes,30);
print(result_3);
System.out.println("\n查找包含西红柿的菜谱:");
Recipe[] result_4=searchRecipeContainsFood(recipes,"西红柿");
print(result_4);
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
enum Huo {
TX(1, "T恤", 245), WQX(2, "网球鞋", 570), WQP(3, "网球拍", 320);
private int id;
private String name;
private double price;
private Huo(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public static Huo getById(int id) {
for (Huo item : Huo.values()) {
if (item.id == id) {
return item;
}
}
return null;
}
}
public class Test {
private static MapInteger, Integer countMap = new HashMapInteger, Integer();
private static double sum = 0;
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("**********************************************");
System.out.println("请选择购买的商品编号:");
for (Huo item : Huo.values()) {
System.out.print(item.getId() + ":" + item.getName() + " ");
}
System.out.println();
System.out.println("**********************************************");
System.out.print("输入购买商品编号:");
int id = in.nextInt();
System.out.print("输入购买商品数量:");
int count = in.nextInt();
Integer tmp = countMap.get(id);
if (tmp == null) {
tmp = 0;
}
countMap.put(id, tmp + count);
show();
while (true) {
System.out.print("是否继续(y/n):");
String jixu = in.next();
if ("Y".equalsIgnoreCase(jixu)) {
break;
} else if ("N".equalsIgnoreCase(jixu)) {
System.out.print("请支付:");
double zhifu = in.nextDouble();
System.out.println("应付金额:" + sum);
System.out.println("客户支付:" + zhifu);
System.out.println("找钱:" + (zhifu - sum));
System.exit(0);
} else {
System.out.print("输入错误");
}
}
}
}
private static void show() {
IteratorInteger it = countMap.keySet().iterator();
while (it.hasNext()) {
Integer key = it.next();
Integer count = countMap.get(key);
Huo huo = Huo.getById(key);
double heji = huo.getPrice() * count;
System.out.println(huo.getName() + "¥" + huo.getPrice() + " 数量:" + count + " 合计:" + heji);
sum += heji;
}
}
}
public class ShopGoodsDemo {
public static void main(String[] args) {
ShopCar s1=new ShopCar(5);
s1.add(new EatFood("面包",12.1));
s1.add(new EatFood("辣条",2.4));
s1.add(new EatFood("饼干",22.3));
s1.add(new WashGoods("洗发水",32.5));
s1.add(new WashGoods("卫生纸",22.8));
print(s1.search("饼干"));
System.out.println("=============");
print(s1.getGoods());
}
public static void print(Goods gs[]){
double sum=0;
for(int i=0;igs.length;i++){
if(gs[i]!=null){
//System.out.println(p[i]+",");
System.out.println(gs[i].getName()+","+gs[i].getPrice());
sum=sum+gs[i].getPrice();
}
}
System.out.println("总价格为:"+sum);
}
}
public interface Goods {
public String getName();
public double getPrice();
}
public class EatFood implements Goods{
private String name;
private double price;
public EatFood() {
}
public EatFood(String name, double price) {
super();
this.name = name;
this.price = price;
}
@Override
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
public class WashGoods implements Goods{
private String name;
private double price;
public WashGoods() {
}
public WashGoods(String name, double price) {
super();
this.name = name;
this.price = price;
}
@Override
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
public class ShopCar {
private Goods goods[]=null;
private int foot;
//数组的大小由程序外部决定
public ShopCar(int len) {
if(len0){
goods=new Goods[len];
}else{
goods=new Goods[1];
}
}
//判断数组的内容是否已满,未满,则添加
public boolean add(Goods g){
if(this.footthis.goods.length){
this.goods[foot]=g;
foot++;
return true;
}else{
return false;
}
}
//关键字查找
public Goods[] search(String keyword){
Goods go[]=null;
int count=0;
for(int i=0;ithis.goods.length;i++){
if(goods[i]!=null){
if(this.goods[i].getName().indexOf(keyword)!=-1){
count++;
}
}
}
go=new Goods[count];
int f=0;
for(int i=0;ithis.goods.length;i++){
if(goods[i]!=null){
if(this.goods[i].getName().indexOf(keyword)!=-1){
go[f]=this.goods[i];
f++;
}
}
}
return go;
}
//得到全部信息
public Goods[] getGoods(){
return this.goods;
}
}
刚帮你调测了一下,当你输入"n"的时候,已经跳出循环了,只不过你没有输出提示,以为没出来,
又输入字符了所有报错了,应该是输入金额的,我帮你加了下输出你可以看下结果;
public static void main(String args[]){
System.out.println("********************");
System.out.println("");
System.out.println("请选择购买商品编号:");
System.out.println("1.T恤(¥245.0) 网球鞋(¥570.0) 网球拍(¥500.0)");
System.out.println("********************");
System.out.println("");
double s=0;
String d;
do{
System.out.println("请输入商品编号:");
Scanner input=new Scanner(System.in);
int a=input.nextInt();
if(a==1){
System.out.println("请输入购买数量:");
int b=input.nextInt();
double c=245*b;
System.out.println("T恤¥245.0 数量:"+b+" "+"合计:"+c);
s=s+c;
}
else if(a==2){
System.out.println("请输入购买数量:");
int b=input.nextInt();
double c=570*b;
System.out.println("网球鞋¥245.0 数量:"+b+" "+"合计:"+c);
s=s+c;
}
else if(a==3){
System.out.println("请输入购买数量:");
int b=input.nextInt();
double c=500*b;
System.out.println("网球拍¥245.0 数量:"+b+" "+"合计:"+c);
s=s+c;
}
else {System.out.println("无此商品");}
System.out.println("否继续(y/n)");
d=input.next();
}while(d.equals("y"));
System.out.println("===========付款===========");
System.out.println("=====输入金额=====");
Scanner input=new Scanner(System.in);
int x=input.nextInt();
double m=s*0.8;
System.out.println("折扣:0.8");
System.out.println("应付金额:"+m);
System.out.println("实际金额:"+x);
double p=x-m;
System.out.println("找钱:"+p);
}