Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使我的代码在输入stop之前一直请求从数组中输入名称_Java_Arrays_Parallel Processing - Fatal编程技术网

Java 如何使我的代码在输入stop之前一直请求从数组中输入名称

Java 如何使我的代码在输入stop之前一直请求从数组中输入名称,java,arrays,parallel-processing,Java,Arrays,Parallel Processing,我需要使我的代码不断要求输入花的名称(从我的数组中的选项),直到“停止”被输入,请记住,我正在使用一个并行数组,也给我的花的价格。最后我需要输入所有的花的总数。谢谢你的帮助 public static void main(String[] args) { String[] flowers = {"petunia", "parse", "rose", "violet", "daisy"}; double[] cost = {.50, .75, 1.50, 1.

我需要使我的代码不断要求输入花的名称(从我的数组中的选项),直到“停止”被输入,请记住,我正在使用一个并行数组,也给我的花的价格。最后我需要输入所有的花的总数。谢谢你的帮助

   public static void main(String[] args) {
        String[] flowers = {"petunia", "parse", "rose", "violet", "daisy"};
        double[] cost = {.50, .75, 1.50, 1.00, .80};
        System.out.println("Please enter a name of one flower ex.");

        for (String f : flowers) {
            System.out.print(f + ",");
        }
        System.out.println("");
        Scanner s1 = new Scanner(System.in);
        System.out.print("please enter your flower :  ");
        String flow;
         String stop = "stop";
        flow = s1.nextLine();
        System.out.println("the flower you chousse si " + flow);

        for (int i = 0; i < flowers.length; i++) {
            double d = cost[i];
            {
                if (flow.equalsIgnoreCase(flowers[i])) {
                    System.out.println("the flowers price " + cost[i]);
                }
              //  else (flow.equalsIgnoreCase(stop)){
                    System.out.println("your total is ");
                }

            }

        }

        // TODO code application logic here
    }

}         
publicstaticvoidmain(字符串[]args){
字符串[]花={“佩妮”、“帕斯”、“玫瑰”、“紫罗兰”、“雏菊”};
双[]成本={.50,75,1.50,1.00,80};
System.out.println(“请输入一朵花的名称”);
用于(字符串f:花){
系统输出打印(f+“,”);
}
System.out.println(“”);
扫描仪s1=新的扫描仪(System.in);
系统输出打印(“请输入您的花:”;
串流;
字符串stop=“stop”;
flow=s1.nextLine();
System.out.println(“你选择的花是”+流);
for(int i=0;i
我想你在期待这样的事情

public static void main(String[] args) {
    String[] flowers = { "petunia", "parse", "rose", "violet", "daisy" };
    double[] cost = { .50, .75, 1.50, 1.00, .80 };
    System.out.println("Please enter a name of one flower ex.");

    for (String f : flowers) {
        System.out.print(f + ",");
    }
    System.out.println("\n");

    Scanner s1 = new Scanner(System.in);

    System.out.print("please enter your flower :  ");
    String flow = s1.nextLine();
    double total = 0.0;

    while (!flow.equals("stop")) {
        System.out.println("the flower you chousse si " + flow);
        for (int i = 0; i < flowers.length; i++) {
            {
                if (flow.equalsIgnoreCase(flowers[i])) {
                    System.out.println("the flowers price " + cost[i]);
                    total += cost[i];
                }
            }
        }
        System.out.print("\nplease enter your flower :  ");
        flow = s1.nextLine();
    }

    System.out.println("\nyour total is " + total);
}
publicstaticvoidmain(字符串[]args){
字符串[]花={“佩妮”、“帕斯”、“玫瑰”、“紫罗兰”、“雏菊”};
双[]成本={.50,75,1.50,1.00,80};
System.out.println(“请输入一朵花的名称”);
用于(字符串f:花){
系统输出打印(f+“,”);
}
System.out.println(“\n”);
扫描仪s1=新的扫描仪(System.in);
系统输出打印(“请输入您的花:”;
字符串流=s1.nextLine();
双倍合计=0.0;
而(!flow.equals(“stop”)){
System.out.println(“你选择的花是”+流);
for(int i=0;i

对于这些类型的场景,您可以使用HashMap键、值对,而不是使用数组。

我想您希望看到类似的情况

public static void main(String[] args) {
    String[] flowers = { "petunia", "parse", "rose", "violet", "daisy" };
    double[] cost = { .50, .75, 1.50, 1.00, .80 };
    System.out.println("Please enter a name of one flower ex.");

    for (String f : flowers) {
        System.out.print(f + ",");
    }
    System.out.println("\n");

    Scanner s1 = new Scanner(System.in);

    System.out.print("please enter your flower :  ");
    String flow = s1.nextLine();
    double total = 0.0;

    while (!flow.equals("stop")) {
        System.out.println("the flower you chousse si " + flow);
        for (int i = 0; i < flowers.length; i++) {
            {
                if (flow.equalsIgnoreCase(flowers[i])) {
                    System.out.println("the flowers price " + cost[i]);
                    total += cost[i];
                }
            }
        }
        System.out.print("\nplease enter your flower :  ");
        flow = s1.nextLine();
    }

    System.out.println("\nyour total is " + total);
}
publicstaticvoidmain(字符串[]args){
字符串[]花={“佩妮”、“帕斯”、“玫瑰”、“紫罗兰”、“雏菊”};
双[]成本={.50,75,1.50,1.00,80};
System.out.println(“请输入一朵花的名称”);
用于(字符串f:花){
系统输出打印(f+“,”);
}
System.out.println(“\n”);
扫描仪s1=新的扫描仪(System.in);
系统输出打印(“请输入您的花:”;
字符串流=s1.nextLine();
双倍合计=0.0;
而(!flow.equals(“stop”)){
System.out.println(“你选择的花是”+流);
for(int i=0;i

对于这些类型的场景,您可以使用HashMap键、值对,而不是使用数组。

如果您使用Map.of、String.join、Map.containsKey和Map.get,您将获得更简洁的Java代码。请看下面

public static void main(String[] args) {
    var flowers = Map.of("petunia", .50, "parse", .75, "rose", 1.50, "violet", 1.00, "daisy", .80);
    System.out.println("Please enter a name of one flower ex.");
    System.out.print(String.join(", ", flowers.keySet()));

    Scanner s1 = new Scanner(System.in);
    double total = 0.0;

    String flow;
    do {
        System.out.print("\nplease enter your flower :  ");
        flow = s1.nextLine();
        var f = flow.toLowerCase();
        if (flowers.containsKey(f)) {
            var cost = flowers.get(f);
            System.out.println("the flowers price " + cost);
            total += cost;
        }
    } while (!flow.equals("stop"));

    System.out.println("\nyour total is " + total);
}

如果使用Map.of、String.join、Map.containsKey和Map.get,您将获得更简洁的Java代码。请看下面

public static void main(String[] args) {
    var flowers = Map.of("petunia", .50, "parse", .75, "rose", 1.50, "violet", 1.00, "daisy", .80);
    System.out.println("Please enter a name of one flower ex.");
    System.out.print(String.join(", ", flowers.keySet()));

    Scanner s1 = new Scanner(System.in);
    double total = 0.0;

    String flow;
    do {
        System.out.print("\nplease enter your flower :  ");
        flow = s1.nextLine();
        var f = flow.toLowerCase();
        if (flowers.containsKey(f)) {
            var cost = flowers.get(f);
            System.out.println("the flowers price " + cost);
            total += cost;
        }
    } while (!flow.equals("stop"));

    System.out.println("\nyour total is " + total);
}

当它等于“停止”时使用break。你需要把你的输入请求放到一个循环中,当用户的输入等于“停止”时,这个循环就会退出。当它等于“停止”时使用break。你需要把你的输入请求放到一个循环中,当用户的输入等于“停止”时,这个循环就会退出。@carlos flores我的回答对你有帮助吗?哥们,那很好用。我完全没有将总值设置为0.0来启动操作。现在我正在努力对两个平行数组进行排序,并在排序后使它们保持平行。@carlos flores我的答案对你有帮助吗?哥们,这很好。我完全没有将总值设置为0.0来启动操作。现在我正在尝试对两个并行数组进行排序,并在排序后使它们保持并行。