Java代码没有显示我想要的结果

时间:2014-05-24 12:35:46

标签: java

我是Java的新手,实际上这段代码是要求用户提供2个数字,用于从英寸到厘米的转换,反之亦然。但是当我构建它时,它说“错误:找不到符号”,虽然我一直在检查代码,但我仍然无法弄明白。以下是我的代码:

import java.util.Scanner;
import java.util.DecimalFormat;

public class Practical2Q2 {

    public Practical2Q2() {}

    public static void inchToCentimeter(double a) {
        System.out.println("Inches      " + "Centimeters");
        System.out.print("\n");

        for (double i = 1.0; i <= a; i++) {
            double cm = i * 2.54;
            System.out.println(i + "        " + df.format(cm));
        }
    }

    public static void centimeterToInch(double b) {
        System.out.print("\n");
        System.out.println("Centimeters     " + "Inches");
        System.out.print("\n");

        for (double i = 5.0; i <= b; i += 5) {
            double inch = i / 2.54;
            System.out.println(i + "        " + df.format(inch));
        }
    }

    public static void main(String args[]) {
        Scanner newScanner = new Scanner(System.in);
        DecimalFormat df = new DecimalFormat("0.00");
        System.out.println("Please enter 2 numbers :\n" + "Number a for the conversion from inch to centimeter,\n"
                + "while number b for the conversion from centimeter to inch");
        System.out.println("Number a : ");
        double c = newScanner.nextDouble();
        System.out.println("Number b : ");
        double d = newScanner.nextDouble();
        inchToCentimeter(c);
        centimeterToInch(d);
    }
}

请你说错了吗?

3 个答案:

答案 0 :(得分:0)

您的变量df仅对您的main方法是本地的。方法inchToCentimeter和centimeterToInch不知道这个变量。要么将其作为全局变量,要么将其作为这些方法的参数传递。

答案 1 :(得分:0)

您的df变量不在那里。在代码中添加以下行。

static DecimalFormat df = new DecimalFormat("0.00");

df变量声明为实例变量。

答案 2 :(得分:0)

您已在主要方法中创建了DecimalFormat df,这意味着您无法在该方法之外访问该方法。我已编辑了您的代码,并且我已在主要方法之外创建了字段DecimalFormat df

我还更改了for loop方法中的centimetersToInch(),现在它打印出inchToCentimeters()方法中的所有值。

以下是代码:

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


public class Main {

static DecimalFormat df;

public static void inchToCentimeter(double a)

{
    System.out.println("Inches      " + "Centimeters");
    System.out.print("\n");

    for (double i = 1.0; i <= a; i++)

    {
        double cm = i * 2.54;
        System.out.println(i + "        " + df.format(cm)); 
    }
} 

public static void centimeterToInch(double b)

{
    System.out.print("\n");
    System.out.println("Centimeters     " + "Inches");
    System.out.print("\n");

    for (double i = 1.0; i <= b; i++)

    {
        double inch = i / 2.54;
        System.out.println(i + "        " + df.format(inch)); 
    }
 }

  public static void main(String args[])

  {
    Scanner newScanner = new Scanner(System.in);
    df = new DecimalFormat("0.00");
    System.out.println("Please enter 2 numbers :\n" + "Number a for the conversion from inch to centimeter,\n"
        + "while number b for the conversion from centimeter to inch");
    System.out.println("Number a : ");
    double c = newScanner.nextDouble();
    System.out.println("Number b : ");
    double d = newScanner.nextDouble();
    inchToCentimeter(c);
    centimeterToInch(d);
  }

}