【问题标题】:How to implement discretionary use of Scanner如何实现 Scanner 的任意使用
【发布时间】:2020-09-18 19:31:07
【问题描述】:

我是 Java 新手,所以如果这是微不足道的,我提前道歉。但是,我有一个简单的计算器,可以用 'b' 美元打印 'a' 项目的成本,但如果你有超过 'c' 项目,那么它需要 'd' 美元。代码是:

Scanner sc = new Scanner(System.in);
    int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(), d = sc.nextInt();

    if (a < b){
        System.out.printf("%d", a * c);
    }
    if (a == b){
        System.out.printf("%d", a * c);

    }
    if (a > b){
        System.out.printf("%d", a * d);
    }

但我想增加一点灵活性并避免歧义,即用户在一行上输入类似a=10 b=5 c=4 d=10 b=5 d=10 a=10 c=4(或任何组合)的内容,两者的输出都是100。所以,我的问题是如何做到让用户输入变量的顺序是任意的,同时保持相同的功能?

TIA

【问题讨论】:

  • 提示:使用有意义的变量名。
  • 我认为有一些解决方法。你可以有一个打印语句让用户知道输入的正确顺序,或者顺序询问输入什么,或者使用条件语句来分析输入,这会增加程序的复杂性。

标签: java


【解决方案1】:

试试这个:

import java.util.Scanner;

public class SimpleCalculatorScanner {
    public static void main(final String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] variables = sc.nextLine().split(" ");
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        for(int i = 0; i < variables.length; i++) {
        try {
            if(variables[i].substring(0, 2).equals("a=")) {
                a=Integer.parseInt(variables[i].substring(2));
            }else if(variables[i].substring(0, 2).equals("b=")) {
                b=Integer.parseInt(variables[i].substring(2));
            }else if(variables[i].substring(0, 2).equals("c=")) {
                c=Integer.parseInt(variables[i].substring(2));
            }else if(variables[i].substring(0, 2).equals("d=")){
                d=Integer.parseInt(variables[i].substring(2));
            }else {
                System.out.println("Unrecognized variable "+variables[i].substring(0, 1)+" detected");
                return;
            }
        }catch(NumberFormatException e) {
            System.out.println("The character you assigned to variable "+variables[i].substring(0, 1)+" isn't a number");
            return;
        }           }
        if (a < b){
            System.out.printf("%d", a * c);
        }
        if (a == b){
            System.out.printf("%d", a * c);

        }
        if (a > b){
            System.out.printf("%d", a * d);
        }
    }
}

示例 I/O

输入

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

输出

100

输入 2

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

输出 2

Unrecognized variable e detected

输入 3

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

输出 3

The character you assigned to variable d isn't a number

工作原理

Scanner 读取一个新行,然后通过拆分每个空格将其转换为一个数组。

一旦变量被存储到一个数组中,我们运行一个for循环来测试数组中每一项的前2个字符是否是a=b=c=d=。如果是a=,则将数组中item的=后面的每个字符解析为整数,赋值给变量a,反之亦然。

如果它不能识别变量,它将打印Unrecognized variable &lt;variablename&gt; detected

如果分配给变量的字符不是数字,它将打印The character you assigned to variable &lt;variablename&gt; isn't a number

【讨论】:

  • 对不起,我的解释有误。它不仅解析数组中项目的最后一个字符 - 它还解析 = 之后的所有字符
【解决方案2】:

您可以执行以下操作:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scannner = new Scanner(System.in);
        System.out.println("Enter the values: ");
        String input = scannner.nextLine();// Scan the complete line

        // Split the input on whitespace
        String[] parts = input.split("\\s+");

        // Create a Map with parts
        Map<String, Integer> map = new HashMap<>();
        for (String part : parts) {
            // Split on '='
            String[] tokens = part.split("=");
            map.put(tokens[0], Integer.valueOf(tokens[1]));
        }

        // The following processing is similar to what you are already doing
        if (map.get("a") < map.get("b")) {
            System.out.printf("%d", map.get("a") * map.get("c"));
        }
        if (map.get("a") == map.get("b")) {
            System.out.printf("%d", map.get("a") * map.get("c"));

        }
        if (map.get("a") > map.get("b")) {
            System.out.printf("%d", map.get("a") * map.get("d"));
        }
    }
}

示例运行:

Enter the values: 
a=10 b=5 c=4 d=10
100

另一个示例运行:

Enter the values: 
b=5 d=10 a=10 c=4
100

注意:此解决方案旨在指导您进步。为了让它变得更好,还有很多工作要做(例如验证、异常处理等)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 2015-08-17
    • 2013-02-02
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    相关资源
    最近更新 更多