如何减少此代码的冗余?

时间:2017-03-12 11:06:24

标签: java

我不允许使用三个以上的打印语句。我尝试通过创建私有方法,但它没有成功。

public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    int numBills1 = spending(console, "John");
    int numBills2 = spending(console, "Jane");
    System.out.println("John needs " + numBills1 + " bills");
    System.out.println("Jane needs " + numBills2 + " bills");
}

public static int spending(Scanner console, String name) {
    System.out.print("How much will " + name + " be spending? ");
    double amount = console.nextDouble();
    System.out.println();
    int numBills = (int) (amount / 20.0);
    if (numBills * 20.0 < amount) {
        numBills++;
    }
    return numBills;
}

6 个答案:

答案 0 :(得分:1)

您可以尝试将输出文本存储在实例变量中,然后对所有输出使用一个print语句。

private static String output="";
public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    spending(console, "John");
    spending(console, "Jane");
    System.out.println(output);
}
public static void spending(Scanner console, String name) {
    System.out.print("How much will " + name + " be spending? ");
    double amount = Double.parseDouble(console.nextLine());
    int numBills = (int) (amount / 20.0);
    if (numBills * 20.0 < amount) {
        numBills++;
    }
    output += name + " needs "+numBills+" bills\n";
}

答案 1 :(得分:0)

如何使用换行\n

System.out.println("John needs " + numBills1 + " bills\nJane needs " + numBills2 + " bills");

您也不需要空打印。删除

System.out.println();

答案 2 :(得分:0)

您有两个解决方案,如果您希望只使用\n连接字符串,则可以只使用两个

public static void main(String[] args) {
    //...
    System.out.println("John needs " + numBills1 + " bills" + "\n" + "Jane needs " + numBills2 + " bills");
    //---------------------------------------------------------^^^----------
}

在您的方法spending中,您可以将print更改为println

public static int spending(Scanner console, String name) {
    System.out.println("How much will " + name + " be spending? ");
    //--------------^^---------------------------------------------

并删除:

System.out.println();

答案 3 :(得分:0)

如前所述,您可以连接两个字符串,并在方法上打印System.out.println("Text here");

此外,您不需要将扫描程序作为方法的参数,您可以简单地在主方法之前键入:

static Scanner output = new Scanner (System.in);

通过这种方式,您可以在整个(整个)程序中使用扫描仪,如下所示:

public static int spending(String name) 
{
        System.out.println("How much will " + name + " be spending? ");
        double amount = output.nextDouble();  

        int numBills = (int) (amount / 20.0);
        if (numBills * 20.0 < amount)
        {
            numBills++;
        }
        return numBills;
}

答案 4 :(得分:0)

像这样的东西

  public static void main(String[] args) {
    while (true) {
        Scanner console = new Scanner(System.in);
        System.out.print("The name : ");
        String name = console.nextLine();
        System.out.print("How much will " + name + " be spending? ");
        double amount = console.nextDouble();
        System.out.println(name + " needs " + calculateBills(amount) + " bills");
    }
}

private static int calculateBills(double amount) {
    int numBills = (int) (amount / 20.0);
    if (numBills * 20.0 < amount) {
        numBills++;
    }
    return numBills;
}

答案 5 :(得分:0)

您似乎想要围捕。一种方法是

int numBills = (int) Math.ceil(amount / 20);
相关问题