首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当输入中有一个空格时,它会复制。

当输入中有一个空格时,它会复制。
EN

Stack Overflow用户
提问于 2016-11-04 22:00:21
回答 1查看 47关注 0票数 0

首先,我想当他们给一个错误的输入,请那错误的输入写在那里的逻辑输入。

当我按下空格时,每一个空格都会重复替换。

喜欢

请输入送货速度。

如果要查看所有的交付速度选项,请输入“选项”。

24小时

输入错误,请阅读选项,并注意大写字母。

请输入送货速度。

如果要查看所有的交付速度选项,请输入“选项”。

输入错误,请阅读选项,并注意大写字母。

请输入送货速度。

如果要查看所有的交付速度选项,请输入“选项”。

123 123 123

输入错误,请阅读选项,并注意大写字母。

请输入送货速度。

如果要查看所有的交付速度选项,请输入“选项”。

输入错误,请阅读选项,并注意大写字母。

请输入送货速度。

如果要查看所有的交付速度选项,请输入“选项”。

输入错误,请阅读选项,并注意大写字母。

请输入送货速度。

如果要查看所有的交付速度选项,请输入“选项”。

这是我的密码。

请帮帮我。

代码语言:javascript
复制
   import java.util.InputMismatchException;
    import java.util.Scanner;
    public class deneme123 {
       public static void main(String[] args){
          String opt=null;
          Scanner input=new Scanner(System.in);
           double cost= 0.0;
          while (true) {
               while (true) {
                   System.out.println("Please enter a delivery speed.\nIf you want to see all options of delivery speed, enter 'options'.");
                  try {
                      opt = input.next();
                     break;
                 }
                 catch (InputMismatchException e) {
                     System.out.println("Wrong type, pls try again!");
                      input.nextLine();
                  }
              }
              if (opt.equals("fast")) {
                  cost = cost + 34.9;
                 System.out.println("Total is updated to " + cost + "$");
                 break;
              }
              else if (opt.equals("average")) {
                  cost = cost + 17.5;
                  System.out.println("Total is updated to " + cost + "$");
                  break;
              }
              else if (opt.equals("standart")) {
                  cost = cost + 5.5;
                  System.out.println("Total is updated to " + cost + "$");
                 break;
                }
                else if (opt.equals("VIP")||opt.equals("vip")||opt.equals("Vip")) {
                   cost = cost + 50;
                  System.out.println("Total is updated to " + cost + "$");
                  break;
               }
                else if (opt.equals("options")) {
                  System.out.println("* For VIP delivery speed enter 'VIP'. (costs 50 $) " +
                           "\n* For fast delivery speed enter 'fast'. (costs 34.9 $) " +
                           "\n* For average delivery speed enter 'average' (costs 17.5 $)" +
                           "\n* For standart delivey speed enter 'standart' (costs 5.5 $)");
               }
              else {
                   System.out.println("Wrong type, please read options and be careful with the capital letters.");
               }
          }
     }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-04 22:54:04

我稍微修改了你的代码。在代码下面,我展示了运行它时得到的输出。

代码语言:javascript
复制
package deneme23;
import java.util.InputMismatchException;
    import java.util.Scanner;
/**
 *
 * @author davec
 */
public class Deneme23 {



       public static void main(String[] args){
          String [] opts=null;
          Scanner input=new Scanner(System.in);
           double cost= 0.0;
           OUTER:
           while (true) {
               while (true) {
                   System.out.println("Please enter a delivery speed.\nIf you want to see all options of delivery speed, enter 'options'.");
                   try {
                       String line = input.nextLine();
                       opts = line.split("\\s");
                       break;
                   }
                   catch (InputMismatchException e) {
                       System.out.println("Wrong type, pls try again!");
                       input.nextLine();
                   }
               }switch (opts[0]) {
                   case "fast":
                       cost = cost + 34.9;
                       System.out.println("Total is updated to " + cost + "$");
                       break OUTER;
                   case "average":
                       cost = cost + 17.5;
                       System.out.println("Total is updated to " + cost + "$");
                       break OUTER;
                   case "standart":
                       cost = cost + 5.5;
                       System.out.println("Total is updated to " + cost + "$");
                       break OUTER;
                   case "VIP":
                   case "vip":
                   case "Vip":
                       cost = cost + 50;
                       System.out.println("Total is updated to " + cost + "$");
                       break OUTER;
                   case "options":
                       System.out.println("* For VIP delivery speed enter 'VIP'. (costs 50 $) " +
                               "\n* For fast delivery speed enter 'fast'. (costs 34.9 $) " +
                               "\n* For average delivery speed enter 'average' (costs 17.5 $)" +
                               "\n* For standart delivey speed enter 'standart' (costs 5.5 $)");
                       break;
                   default:
                       System.out.println("Wrong type, please read options and be careful with the capital letters.");
                       break;
               }
           }
     }
    } 

这是我运行它时得到的输出:

代码语言:javascript
复制
Please enter a delivery speed.
If you want to see all options of delivery speed, enter 'options'.
24 hours
Wrong type, please read options and be careful with the capital letters.
Please enter a delivery speed.
If you want to see all options of delivery speed, enter 'options'.
options
* For VIP delivery speed enter 'VIP'. (costs 50 $) 
* For fast delivery speed enter 'fast'. (costs 34.9 $) 
* For average delivery speed enter 'average' (costs 17.5 $)
* For standart delivey speed enter 'standart' (costs 5.5 $)
Please enter a delivery speed.
If you want to see all options of delivery speed, enter 'options'.
fast
Total is updated to 34.9$
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40432311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档