Java 为什么我必须输入两次数据?

Java 为什么我必须输入两次数据?,java,methods,console,local-variables,Java,Methods,Console,Local Variables,我正在写这个程序,它可以让你输入每个季度、一角硬币、五分镍币和一分钱的金额,并以美元返回它们的金额。之后,我希望能够将他们生产的美元数量相加,而无需再次输入所有硬币数量。有什么想法吗?提前谢谢 import java.util.*; public class HalfDollar { public static final Scanner CONSOLE = new Scanner(System.in); public static void main(String[] args)

我正在写这个程序,它可以让你输入每个季度、一角硬币、五分镍币和一分钱的金额,并以美元返回它们的金额。之后,我希望能够将他们生产的美元数量相加,而无需再次输入所有硬币数量。有什么想法吗?提前谢谢

import java.util.*;

public class HalfDollar {
  public static final Scanner CONSOLE = new Scanner(System.in);

  public static void main(String[] args) {
    quarterDollarAmount( );
    dimeDollarAmount( );
    nickelDollarAmount( );
    pennyDollarAmount( );
    totalDollarAmount( );
  }

  public static double quarterDollarAmount( ) {
    System.out.print("Enter the number of quarters: ");
    int quarterDollar = CONSOLE.nextInt( );
    double amount = quarterDollar * 0.25;
    System.out.println(quarterDollar + " Quarter are $" + amount);
    return amount;
  }

  public static double dimeDollarAmount( ) {
    System.out.print("Enter the number of dimes: ");
    int dimeDollar = CONSOLE.nextInt( );
    double amount = dimeDollar * 0.10;
    System.out.println(dimeDollar + " Dimes are $" + amount);
    return amount;
  }

  public static double nickelDollarAmount( ) {
    System.out.print("Enter the number of nickels: ");
    int nickelDollar = CONSOLE.nextInt( );
    double amount = nickelDollar * 0.05;
    System.out.println(nickelDollar + " Nickels are $" + amount);
    return amount;
  }

  public static double pennyDollarAmount( ) {
    System.out.print("Enter the number of pennies: ");
    int pennyDollar = CONSOLE.nextInt( );
    double amount = pennyDollar * 0.01;
    System.out.println(pennyDollar + " Pennies are $" + amount);
    return amount;
  }

  public static double totalDollarAmount( ) {
    double quarter = quarterDollarAmount();
    double dime = dimeDollarAmount();
    double nickel = nickelDollarAmount();
    double penny = pennyDollarAmount();
    double total = quarter + dime + nickel + penny;
    System.out.println("Total amount is $" + total);
    return total;
  }
}

你没有对你的变量做任何事情。只是打电话给他们,然后他们就离开了范围

您可以将返回的值存储在全局变量中以供以后使用

private double quarter, dime, total;

public static void main(String[] args) {
     quarter = quarterDollarAmount();
     dime = dimeDollarAmount();
     total = (quarter + dime);
     s.o.p(total);
}

如果打印出来后不关心值,可以使用局部变量对其进行合计,也可以按如下方式对方法进行合计

public static void main(String[] args) {
     s.o.p(quarterDollarAmount( ) + dimeDollarAmount( ) + ....);
}
要将值设置为小数点后2位,请使用以下内容:

DecimalFormat format = new DecimalFormat("#.00");
Double total_formatted = Double.parseDouble(format.format(total));
s.o.p(total_formatted);
这将强制该值有2个小数位,小数位的剩余位数为可选数量


最后一件事,你可能不想让一切都是静态的。它基本上克服了面向对象的观点,因为静态变量将贯穿于类的所有对象。

Hmmm,我觉得这就像是一个家庭作业问题。如果您真的是编写此代码的人,那么任何数量的解决方案都应该是显而易见的。但不管怎样,我不会评判你的旅程。由于您要从每个方法返回金额,因此只需在执行过程中保持所有金额的运行总计,然后更改totalDollarAmount方法,将总计作为输入,而不是再次请求:

double total = 0.0;
total += quarterDollarAmount( );
total += dimeDollarAmount( );
total += nickelDollarAmount( );
total += pennyDollarAmount( );
totalDollarAmount( total );