如何更改代码以使程序在用户输入否之后跳过选项?

发布于 2025-01-25 01:47:50 字数 1331 浏览 3 评论 0原文

我希望该程序跳过“输入另一个?”?当提示“明确”后输入n时。我该怎么做? 现在,当输入n时,它仍然会提示“长距离?”

以下是当前一批代码。 上下文:这是代码的数据输入部分,其中用户输入被添加到arraylist。

String input=null;
    
    //data entry (user input + adding to arraylist)
    do {
    
        System.out.println("Enter parcel code: ");
        String pCode=kboard.next();
        
        System.out.println("Enter parcel length: ");
        double pLength=kboard.nextDouble();
        
        System.out.println("Enter parcel width: ");
        double pWidth=kboard.nextDouble();
        
        System.out.println("Enter parcel height: ");
        double pHeight=kboard.nextDouble();
        
        System.out.println("Enter parcel weight: ");
        double pWeight=kboard.nextDouble();
        
        Parcel parcel=new Parcel(pCode, pLength, pWidth, pHeight, pWeight);
        
        List.add(parcel);
        
        System.out.println("Express parcel? Y/N: ");
        String expressP=kboard.nextLine();
        input=kboard.next();
        
        System.out.println("Long distance parcel? Y/N: ");
        String longDist=kboard.nextLine();
        input=kboard.next();
        
        System.out.println("Do you want to enter another parcel record? Y/N: ");
        input=kboard.next();
        
    }
    
    while (input.toLowerCase().equals("y"));

I want the program to skip to "Enter another?" when N is entered after being prompted "Express?". How can I do that?
Now, when N is entered, it still prompts "Long distance?"

Below is the current batch of code. Context: this is the data entry part of the code where user input gets added to an arraylist.

String input=null;
    
    //data entry (user input + adding to arraylist)
    do {
    
        System.out.println("Enter parcel code: ");
        String pCode=kboard.next();
        
        System.out.println("Enter parcel length: ");
        double pLength=kboard.nextDouble();
        
        System.out.println("Enter parcel width: ");
        double pWidth=kboard.nextDouble();
        
        System.out.println("Enter parcel height: ");
        double pHeight=kboard.nextDouble();
        
        System.out.println("Enter parcel weight: ");
        double pWeight=kboard.nextDouble();
        
        Parcel parcel=new Parcel(pCode, pLength, pWidth, pHeight, pWeight);
        
        List.add(parcel);
        
        System.out.println("Express parcel? Y/N: ");
        String expressP=kboard.nextLine();
        input=kboard.next();
        
        System.out.println("Long distance parcel? Y/N: ");
        String longDist=kboard.nextLine();
        input=kboard.next();
        
        System.out.println("Do you want to enter another parcel record? Y/N: ");
        input=kboard.next();
        
    }
    
    while (input.toLowerCase().equals("y"));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他是夢罘是命 2025-02-01 01:47:50

在接受' express包裹'的输入后,使用' if-else '条件

   System.out.println("Express parcel? Y/N: ");
   String expressP=kboard.nextLine();
   input=kboard.next();

   if(ip.equalsIgnoreCase("Y").  //This will execute only if previous resp is Yes
  { 
       System.out.println("Long distance parcel? Y/N: ");
       String longDist=kboard.nextLine();
      input=kboard.next();
  }

  System.out.println("Do you want to enter another parcel record? Y/N: ");
  input=kboard.next();

,如果用户输入n或n,则会要求下一个迭代跳过包裹代码距离。

Use 'if-else' conditioning after accepting the input for 'Express Parcel'

   System.out.println("Express parcel? Y/N: ");
   String expressP=kboard.nextLine();
   input=kboard.next();

   if(ip.equalsIgnoreCase("Y").  //This will execute only if previous resp is Yes
  { 
       System.out.println("Long distance parcel? Y/N: ");
       String longDist=kboard.nextLine();
      input=kboard.next();
  }

  System.out.println("Do you want to enter another parcel record? Y/N: ");
  input=kboard.next();

This way if user enter N or n it'll ask for next iteration skipping the code for Parcel distance.

陌路黄昏 2025-02-01 01:47:50

尝试以下代码:

  String input=null;

    //data entry (user input + adding to arraylist)
    do {

        System.out.println("Enter parcel code: ");
        String pCode=kboard.next();

        System.out.println("Enter parcel length: ");
        double pLength=kboard.nextDouble();

        System.out.println("Enter parcel width: ");
        double pWidth=kboard.nextDouble();

        System.out.println("Enter parcel height: ");
        double pHeight=kboard.nextDouble();

        System.out.println("Enter parcel weight: ");
        double pWeight=kboard.nextDouble();

        /*Parcel parcel=new Parcel(pCode, pLength, pWidth, pHeight, pWeight);

        List.add(parcel);*/

        System.out.println("Express parcel? Y/N: ");
        String expressP=kboard.next();
        if (expressP.equalsIgnoreCase("Y")) {

            System.out.println("Long distance parcel? Y/N: ");
            String longDist = kboard.nextLine();
            input = kboard.next();
        }
        System.out.println("Do you want to enter another parcel record? Y/N: ");
        input=kboard.next();

    }

    while (input.toLowerCase().equals("y"));

如果检查用户已输入y,则需要添加额外的代码

Try the below code :

  String input=null;

    //data entry (user input + adding to arraylist)
    do {

        System.out.println("Enter parcel code: ");
        String pCode=kboard.next();

        System.out.println("Enter parcel length: ");
        double pLength=kboard.nextDouble();

        System.out.println("Enter parcel width: ");
        double pWidth=kboard.nextDouble();

        System.out.println("Enter parcel height: ");
        double pHeight=kboard.nextDouble();

        System.out.println("Enter parcel weight: ");
        double pWeight=kboard.nextDouble();

        /*Parcel parcel=new Parcel(pCode, pLength, pWidth, pHeight, pWeight);

        List.add(parcel);*/

        System.out.println("Express parcel? Y/N: ");
        String expressP=kboard.next();
        if (expressP.equalsIgnoreCase("Y")) {

            System.out.println("Long distance parcel? Y/N: ");
            String longDist = kboard.nextLine();
            input = kboard.next();
        }
        System.out.println("Do you want to enter another parcel record? Y/N: ");
        input=kboard.next();

    }

    while (input.toLowerCase().equals("y"));

you need to add an extra if to check the user has entered Y or not

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文