简体   繁体   English

如何使用数据列表检查多行输入

[英]How to Check multiple lines of input with a list of data

I am trying to get java to take an input of x amount of lines and get it to check an if else tree for the output of each one. 我正在尝试让java输入x的行数,并检查每一个输出的if else树。 I can only get it to check one line of input though. 我只能让它检查输入的一行。 Please help, I am new to programming Java. 请帮助,我是Java编程的新手。 This is what I have so fat: 这就是我这么胖:

package Javatest;
import java.util.Scanner;

public class Jtest {

  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("Enter Part Number: ");
    String price = input.nextLine();

    if("700212129".equals(price)){
        System.out.println("$280.00");
    }else{
      if("100010B".equals(price)){
        System.out.println("$52.50");
      }else{
        if("100030B".equals(price)){
          System.out.println("$7.18");
        }else{
          if("1189A-BELDEN".equals(price)){
            System.out.println("$140.00");
          }else{
            System.out.println("#N/A");
            return;     
          }             
        }
      }
    }
  }
}

Try to extract logic of each part. 尝试提取每个部分的逻辑。 This is an implementation which separately initializes the list, waits for input, scrolls down stuffs and searches for code to extract the price. 这是一个单独初始化列表,等待输入,向下滚动内容并搜索代码以提取价格的实现。

package Javatest;

import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;

public class Jtest {

    private static final String EXIT_CODE = "exit"; 
    private Set<Stuff> stuffs = new LinkedHashSet<Stuff>();

    public static void main(String[] args) {
        Jtest jtest = new Jtest();
        jtest.initList();
        while(true) {
            System.out.println("Enter Part Number: ");
            Scanner input = new Scanner(System.in);
            String inputLine = input.nextLine();
            if(inputLine.equals(EXIT_CODE)){
                System.out.println("Exiting");
                break;
            }
            String price = jtest.findPrice(inputLine);
            System.out.println("Price: " + price);
        }
    }

    private void initList() {
        stuffs.add(new Stuff("700212129", "$280.00"));
        stuffs.add(new Stuff("100010B", "$52.50"));
        stuffs.add(new Stuff("100030B", "$7.18"));
        stuffs.add(new Stuff("1189A-BELDEN", "$140.00"));
    }

    private String findPrice(String code) {
        for(Stuff stuff: stuffs){
            if(code.equals(stuff.getCode())){
                return stuff.getPrice();
            }
        }
        return "#N/A";      
    }

    public static class Stuff {

        private String code;
        private String price;

        public Stuff(String code, String price) {
            this.code = code;
            this.price = price;
        }

        public String getPrice(){
            return this.price;          
        }

        public String getCode(){
            return this.code;           
        }
    }
}

This is a very basic implementation but it can inspire you to build a better solution. 这是一个非常基本的实现,但是它可以激发您构建更好的解决方案。

the first thing you want to do is learn about splitting/decoupling methods from your main. 您要做的第一件事是从您的主机了解拆分/解耦方法。 Its bad practise to just write all of your code in one place. 不好的习惯是只在一个地方编写所有代码。

What i would do is write this in two parts: 我要做的是将其分为两个部分:

First create a procedure to return a string from a single string input and include your nested if statement inside. 首先创建一个过程以从单个字符串输入返回一个字符串,并将嵌套的if语句包含在内。

private static string GetString(string inputData)
{
         if("700212129".equals(inputData)){
            return "$280.00";
        }else{
            if("100010B".equals(inputData)){
                return "$52.50";
            }else{
                if("100030B".equals(inputData)){
                    return "$7.18";
                }else{
                    if("1189A-BELDEN".equals(inputData)){
                       return "$140.00";
                    }else{
                        return "#N/A";

      }               
}

Then in Main(), use a loop to constantly check any input you have typed, using a While loop would be a good example as you can use an escape clause to stop the loop/program. 然后在Main()中,使用循环不断检查您键入的任何输入,使用While循环将是一个很好的示例,因为您可以使用转义子句来停止循环/程序。

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("Enter Part Number: ");
    String value = input.nextLine();
    String output;
    While(value != "E")
     {
      output = GetString(value);
      System.out.println("Value is: " + output);
     }
}

I would also suggest changing your nested If statement to a Switch/Select statement. 我还建议将嵌套的If语句更改为Switch / Select语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM