我如何计算某些值是双倍的值?

时间:2014-09-28 16:01:01

标签: java

我编写的代码编译得很好,但有些值无法计算。费率和小时值,学费和.08不计算。也许我的代码是错的或什么的。我还是java的新手,所以我尽我所能。这是我的代码:

import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Tuition
{
    static double fees;
    static double rate;
    static double tuition;
    private static Scanner sc;
    public static void main(String[] args) throws IOException
    {
        //declare variables
        int hours;

        //call methods
        displayWelcome();
    hours = getHours();
    rate = getRate(hours);
    tuition = calcTuition(hours, rate);
    fees = calcFees(tuition);
}

public static void displayWelcome()
{

    //welcome statement
    System.out.println("Welcome to school that offers distance learning courses");
}

public static int getHours()
{
    //declare method variables
    int hours = 0;

    //a user must enter a string value
    System.out.println("Enter total number of hours");
    sc = new Scanner(System.in);

    try
    {
        hours = sc.nextInt();
    }
    catch(NumberFormatException e)
    {
        System.out.print("Incorrect number");
    }
    return hours;
}

public static double getRate(int hours)
{
    if (hours > 15)
    {
        System.out.println("rate per credit hour is 44.50");
    }
    else
    {
        System.out.println("rate per credit hour is 50.00");
    }
    return rate;
}

public static double calcTuition(int hours, double rate)
    {
        tuition =(double)rate * hours;
        System.out.print("tuition is" + (tuition));
        return tuition;
    }

public static double calcFees(double tuition)
    {
        fees =(double)tuition * .08;
        System.out.print("fees are" + (fees));
        return fees;
    }

public static void displayToatal(double total)
    {
        DecimalFormat twoDigits = new DecimalFormat("$#,000.00");

        System.out.println("the total value is"+ twoDigits.format(tuition + fees));
    }

}

2 个答案:

答案 0 :(得分:0)

calcTuition()

tuition =(double)rate * hours;

应该是

tuition =rate * (double)hours;

:)

答案 1 :(得分:0)

您没有设置费率。您需要一行看起来像" rate = x;"的代码。您只能在getRate方法中使用println,但实际上并未将变量设置为等于任何内容。