找不到符号:变量错误

时间:2015-10-30 19:21:11

标签: java variables find symbols

这是我的代码。我在编程类中,似乎我遇到了一大堆错误,我不知道如何修复。他们都这么说:

C:\ Users \ patrickwilliams \ Desktop \ Java \ tuition.java:30:找不到符号 符号:变量小时 地点:上课学费     小时= 0;

每个变量的

等等

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


public class tuition {
  public static void main(String[] args) {
    int hours;
    double fees, rate, tuition;



    displayWelcome();
    hours = getHours();
    rate = getRate(hours);
    tuition = calcTuition(hours, rate);
    fees = calcFees(tuition);
    displayTotal(tuition + fees);

  }

  public static void displayWelcome() {

    System.out.println("Welcome to the tuition calculator!");

  }

  public static int getHours() {
    String strHours;
    hours = 0;
    boolean done = false;



    while (!done) {
      try {
        int hours;

        System.out.println("Enter the total number of hours.");
        hours = Input.nextInt();

        done = true;
      } catch (Exception e) {
        System.out.println("Please enter an integer value.");
      }
    }
    return hours;
  }

  public static double getRate(int hours) {

    if (hours > 15) {
      rate = 44.5;
    } else {
      rate = 50;
    }
    return rate;
  }

  public static double calcTuition(int hours, double rate) {
    tuition = hours * rate;
    return tuition;
  }

  public static double calcFees(double tuition) {

    fees = tuition * .08;
    return fees;
  }

  public static void displayTotal(double total) {

    DecimalFormat twoDigits = new DecimalFormat("$#,000.00");

    System.out.println("Your total is " + twoDigits.format(tuition + fees));

  }
}

1 个答案:

答案 0 :(得分:0)

public static int getHours() {
    String strHours;
    hours = 0;
    ...

这是您的问题hours。在尝试为其添加值之前,不会声明此变量。

扩大一点:

在java中,您需要在函数内部声明变量,然后才能使用它。稍后在同一个功能中你有

while (!done) {
  try {
    int hours;
...

int hours声明变量在此函数中使用,但在您的情况下,您尝试在声明之前使用它。删除此int hours;行并在int之前添加hours = 0;应清除此错误。