简体   繁体   English

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

[英]Why do I have to enter in data twice?

I'm writing this program that will let you enter in an amount for each quarters, dimes, nickels and pennies and return their amount in $. 我正在编写这个程序,让你输入每个季度,硬币,镍币和便士的金额,并以$为单位返还金额。 After that, I want to be able to add up the dollar amounts they produce without having to enter in all of the coin amounts a second time. 在那之后,我希望能够将他们生产的美元金额相加,而无需再次输入所有硬币金额。 Any ideas? 有任何想法吗? Thanks in advance. 提前致谢。

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;
  }
}

You're not doing anything with your variables. 你没有对你的变量做任何事情。 Just calling them and then they're moving out of scope. 只是打电话给他们然后他们超出了范围。

You could either store the returned value in a global variable to use later. 您可以将返回的值存储在全局变量中以便稍后使用。

private double quarter, dime, total;

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

} }

If you don't care about the value after printing it out you can either total them up with local variables or literally just total up your methods as follows. 如果您在打印后不关心该值,您可以使用局部变量对其进行总计,或者按照以下方法总计您的方法。

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

To get your value to 2 decimal places use something like the following: 要使您的值达到2位小数,请使用以下内容:

DecimalFormat format = new DecimalFormat("#.00");
Double total_formatted = Double.parseDouble(format.format(total));
s.o.p(total_formatted);

That enforces the value to have 2 decimal places with an optional amount of digits left of the decimal place. 这会强制该值具有2个小数位,并且可选的小数位数位数。

Final thing, you probably don't want to make everything static. 最后,你可能不想让一切都变得静止。 It basically defeats the point of object orientation as static variable will persist throughout all objects of a class. 它基本上打败了面向对象的点,因为静态变量将在类的所有对象中持久存在。

Hmmm, this smells to me like a homework problem. 嗯,这对我来说就像是一个家庭作业问题。 If you are truly the one who wrote this code, any number of solutions should be pretty obvious. 如果您真的是编写此代码的人,那么任何数量的解决方案都应该非常明显。 But whatever, I won't judge your journey. 但无论如何,我不会判断你的旅程。 Since you are returning the amount from each method, just keep a running total of all the amounts as you go along, then change your totalDollarAmount method to take the total as input instead of asking for it again: 由于您从每个方法返回金额,只需保持所有金额的运行总计,然后更改totalDollarAmount方法以将总计作为输入而不是再次要求它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM