解析用户输入

时间:2018-04-19 18:16:29

标签: java

如果用户输入“add 5”,我想添加一个数字,如果用户输入“set 5”,我想初始化变量。我怎么能在java中做到这一点?

  import java.io.*;
  import java.math.*;
  import java.text.*;
  import java.util.*;
  import java.util.regex.*;

   public class Solution {

    /*
 * Complete the maximumProgramValue function below.
 */
 static long maximumProgramValue(int n) {
    /*
     * Write your code here.
     */

}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
    BufferedWriter bufferedWriter = new BufferedWriter(new 
                            FileWriter(System.getenv("OUTPUT_PATH")));

    int n = scanner.nextInt();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])*");

    long result = maximumProgramValue(n);

    bufferedWriter.write(String.valueOf(result));
    bufferedWriter.newLine();

    bufferedWriter.close();

    scanner.close();
   }
}

帮助我解决它,我怎么能用它来得到答案,我已经被困在这里很久了,请尽快帮助谢谢

1 个答案:

答案 0 :(得分:0)

我没有抓住你的代码,但这是你问题的答案

import java.util.*;

public class aaaaa {

private static final Scanner scanner = new Scanner(System.in);

public static void main(final String[] args) {
    String n = scanner.next();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])*");

       if(n.equals("add 5")){
          //add number here
       }
       else if (n.equals("set 5")){
            //set the number here
       }


} 


}