如何实现Scanner的自由使用

huangapple go评论96阅读模式
英文:

How to implement discretionary use of Scanner

问题

我是Java的新手,所以如果这显得很简单,我提前道歉。但是,我有一个简单的计算器,按照每个物品“a”的价格为每个物品“b”美元打印成本,但是如果你有超过“c”的物品,那么它的成本是“d”美元。对应的代码如下:

  1. Scanner sc = new Scanner(System.in);
  2. int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(), d = sc.nextInt();
  3. if (a < b){
  4. System.out.printf("%d", a * c);
  5. }
  6. if (a == b){
  7. System.out.printf("%d", a * c);
  8. }
  9. if (a > b){
  10. System.out.printf("%d", a * d);
  11. }

但我想增加一些灵活性并避免歧义,也就是说,用户输入类似 a=10 b=5 c=4 d=10b=5 d=10 a=10 c=4(或任何组合),都在一行上进行,输出都为 100。因此,我的问题是,如何使用户输入变量的顺序是任意的,同时保持相同的功能?

谢谢。

英文:

I'm a Java newbie, so I apologize in advance if this is trivial. But, I have a simple calculator that prints the cost of 'a' items by 'b' dollars, but if you have more than 'c' items, then it costs 'd' dollars. The code for that is:

  1. Scanner sc = new Scanner(System.in);
  2. int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(), d = sc.nextInt();
  3. if (a &lt; b){
  4. System.out.printf(&quot;%d&quot;, a * c);
  5. }
  6. if (a == b){
  7. System.out.printf(&quot;%d&quot;, a * c);
  8. }
  9. if (a &gt; b){
  10. System.out.printf(&quot;%d&quot;, a * d);
  11. }

but I'd like to add a bit more flexibility and avoid ambiguity, that is, the user inputs something like a=10 b=5 c=4 d=10 or b=5 d=10 a=10 c=4 (or any combination) all on one line, and the output is 100 for both. So, my question is how can I make it so that the order in which the user inputs the variables is arbitrary, all whilst keeping the same functionality?

TIA

答案1

得分: 1

你可以按照以下方式进行操作:

  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7. System.out.println("输入数值:");
  8. String input = scanner.nextLine(); // 扫描整行输入
  9. // 使用空白字符拆分输入
  10. String[] parts = input.split("\\s+");
  11. // 创建一个带有部分的映射
  12. Map<String, Integer> map = new HashMap<>();
  13. for (String part : parts) {
  14. // 在'='处拆分
  15. String[] tokens = part.split("=");
  16. map.put(tokens[0], Integer.valueOf(tokens[1]));
  17. }
  18. // 下面的处理与您已经在做的类似
  19. if (map.get("a") < map.get("b")) {
  20. System.out.printf("%d", map.get("a") * map.get("c"));
  21. }
  22. if (map.get("a") == map.get("b")) {
  23. System.out.printf("%d", map.get("a") * map.get("c"));
  24. }
  25. if (map.get("a") > map.get("b")) {
  26. System.out.printf("%d", map.get("a") * map.get("d"));
  27. }
  28. }
  29. }

示例运行:

  1. 输入数值:
  2. a=10 b=5 c=4 d=10
  3. 100

另一个示例运行:

  1. 输入数值:
  2. b=5 d=10 a=10 c=4
  3. 100

注意: 这个解决方案是为了引导您的进展。还有很多工作要做(例如验证、异常处理等),以使其更好。

英文:

You can do something like as follows:

  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner scannner = new Scanner(System.in);
  7. System.out.println(&quot;Enter the values: &quot;);
  8. String input = scannner.nextLine();// Scan the complete line
  9. // Split the input on whitespace
  10. String[] parts = input.split(&quot;\\s+&quot;);
  11. // Create a Map with parts
  12. Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();
  13. for (String part : parts) {
  14. // Split on &#39;=&#39;
  15. String[] tokens = part.split(&quot;=&quot;);
  16. map.put(tokens[0], Integer.valueOf(tokens[1]));
  17. }
  18. // The following processing is similar to what you are already doing
  19. if (map.get(&quot;a&quot;) &lt; map.get(&quot;b&quot;)) {
  20. System.out.printf(&quot;%d&quot;, map.get(&quot;a&quot;) * map.get(&quot;c&quot;));
  21. }
  22. if (map.get(&quot;a&quot;) == map.get(&quot;b&quot;)) {
  23. System.out.printf(&quot;%d&quot;, map.get(&quot;a&quot;) * map.get(&quot;c&quot;));
  24. }
  25. if (map.get(&quot;a&quot;) &gt; map.get(&quot;b&quot;)) {
  26. System.out.printf(&quot;%d&quot;, map.get(&quot;a&quot;) * map.get(&quot;d&quot;));
  27. }
  28. }
  29. }

A sample run:

  1. Enter the values:
  2. a=10 b=5 c=4 d=10
  3. 100

Another sample run:

  1. Enter the values:
  2. b=5 d=10 a=10 c=4
  3. 100

Note: This solution is to guide you progress. There is a lot to be done (e.g. validations, exception handling etc.) in order to make it better.

答案2

得分: 1

  1. 尝试这个
  2. import java.util.Scanner;
  3. public class SimpleCalculatorScanner {
  4. public static void main(final String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. String[] variables = sc.nextLine().split(" ");
  7. int a = 0;
  8. int b = 0;
  9. int c = 0;
  10. int d = 0;
  11. for(int i = 0; i < variables.length; i++) {
  12. try {
  13. if(variables[i].substring(0, 2).equals("a=")) {
  14. a=Integer.parseInt(variables[i].substring(2));
  15. }else if(variables[i].substring(0, 2).equals("b=")) {
  16. b=Integer.parseInt(variables[i].substring(2));
  17. }else if(variables[i].substring(0, 2).equals("c=")) {
  18. c=Integer.parseInt(variables[i].substring(2));
  19. }else if(variables[i].substring(0, 2).equals("d=")){
  20. d=Integer.parseInt(variables[i].substring(2));
  21. }else {
  22. System.out.println("无法识别的变量 "+variables[i].substring(0, 1)+" 检测到");
  23. return;
  24. }
  25. }catch(NumberFormatException e) {
  26. System.out.println("你分配给变量 "+variables[i].substring(0, 1)+" 的字符不是一个数字");
  27. return;
  28. }
  29. }
  30. if (a < b){
  31. System.out.printf("%d", a * c);
  32. }
  33. if (a == b){
  34. System.out.printf("%d", a * c);
  35. }
  36. if (a > b){
  37. System.out.printf("%d", a * d);
  38. }
  39. }
  40. }
  41. **示例输入/输出**
  42. *输入*
  43. b=5 d=10 a=10 c=4
  44. *输出*
  45. 100
  46. *输入 2*
  47. b=5 d=10 e=10 c=4
  48. **输出 2**
  49. 无法识别的变量 e 检测到
  50. *输入 3*
  51. a=10 b=5 c=4 d=iforgot
  52. *输出 3*
  53. 你分配给变量 d 的字符不是一个数字
  54. **工作原理**
  55. Scanner读取一行然后通过分割每个空格将其转换为数组
  56. 一旦变量被存储到数组中我们运行一个**for**循环来测试数组中每个项的前两个字符是否为`a=``b=``c=``d=`如果是`a=`则将`=`后的每个字符解析为整数并将其赋值给变量a依此类推
  57. 如果它无法识别变量它将打印出`无法识别的变量 <variablename> 检测到`
  58. 如果分配给变量的字符不是数字它将打印出`你分配给变量 <variablename> 的字符不是一个数字`
英文:

Try this:

  1. import java.util.Scanner;
  2. public class SimpleCalculatorScanner {
  3. public static void main(final String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. String[] variables = sc.nextLine().split(&quot; &quot;);
  6. int a = 0;
  7. int b = 0;
  8. int c = 0;
  9. int d = 0;
  10. for(int i = 0; i &lt; variables.length; i++) {
  11. try {
  12. if(variables[i].substring(0, 2).equals(&quot;a=&quot;)) {
  13. a=Integer.parseInt(variables[i].substring(2));
  14. }else if(variables[i].substring(0, 2).equals(&quot;b=&quot;)) {
  15. b=Integer.parseInt(variables[i].substring(2));
  16. }else if(variables[i].substring(0, 2).equals(&quot;c=&quot;)) {
  17. c=Integer.parseInt(variables[i].substring(2));
  18. }else if(variables[i].substring(0, 2).equals(&quot;d=&quot;)){
  19. d=Integer.parseInt(variables[i].substring(2));
  20. }else {
  21. System.out.println(&quot;Unrecognized variable &quot;+variables[i].substring(0, 1)+&quot; detected&quot;);
  22. return;
  23. }
  24. }catch(NumberFormatException e) {
  25. System.out.println(&quot;The character you assigned to variable &quot;+variables[i].substring(0, 1)+&quot; isn&#39;t a number&quot;);
  26. return;
  27. } }
  28. if (a &lt; b){
  29. System.out.printf(&quot;%d&quot;, a * c);
  30. }
  31. if (a == b){
  32. System.out.printf(&quot;%d&quot;, a * c);
  33. }
  34. if (a &gt; b){
  35. System.out.printf(&quot;%d&quot;, a * d);
  36. }
  37. }
  38. }

Sample I/O

Input

  1. b=5 d=10 a=10 c=4

Output

  1. 100

Input 2

  1. b=5 d=10 e=10 c=4

Output 2

  1. Unrecognized variable e detected

Input 3

  1. a=10 b=5 c=4 d=iforgot

Output 3

  1. The character you assigned to variable d isn&#39;t a number

How it works

The Scanner reads a new line, and then turns it into an array by splitting every space.

Once the variables have been stored into an array, we run a for loop to test whether the first 2 characters of each item of the array are a=, b=, c= or d=. If it is a=, then it parses the every character after the = of the item in the array into an integer, and assigns it to variable a, vice versa.

If it doesn't recognize the variable, it will print Unrecognized variable &lt;variablename&gt; detected.

If the character assigned to the variable wasn't a number, it will print The character you assigned to variable &lt;variablename&gt; isn&#39;t a number

huangapple
  • 本文由 发表于 2020年9月19日 03:31:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63961766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定