进口?使用包

时间:2012-03-28 23:26:47

标签: java

我正在做作业,我很接近,但我遇到了问题。我刚刚学会了如何在eclipse中使用包,所以我有一个类从包中导入另一个类(我想我说对了) 主要提示用户输入介于-100和100之间的整数,我遇到验证问题。我知道问题出在我导入的地方我只是不确定我需要去解决它的方向。

这是我主要代码的一部分。 (如果你想跳过,我的问题从最后几行开始)

    import myUtils.util.Console;

    public class ConsoleTestApp
    {
         public static void main(String args[])
         {
             // create the Console object
             Console c = new Console();

            // display a welcome message
            c.println("Welcome to the Console Tester application");
            c.println();

            // int
            c.println("Int Test");
            int i = c.getIntWithinRange("Enter an integer between -100 and 100: ", -101, 101);
            c.println();

在最后一行代码中

    int i = c.

我在i下得到一条波浪线,告诉我我没有使用局部变量所以我不确定在这种情况下究竟是什么解决了,因为我试图在另一个类中使用它。我需要创建一个对象吗?

我有一个名为Console的类,它位于另一个我认为已正确导入的包中。 这是我在我的控制台类中停留的代码。

package myUtils.util;

import java.util.Scanner;

public class Console 

{
Scanner sc = new Scanner(System.in);
public void print(String s)
{
    System.out.println();
}

public void println(String s)
{
    System.out.println();

}

public void println()
{
    System.out.println();

}

public int getIntWithinRange(String prompt, int min, int max)
{

    int i = 0;
    boolean isValid = false;
    while (isValid == false)
    {
        System.out.println(prompt);
        if (sc.hasNextInt())
        {

            i = sc.nextInt();
                if (i < min)
                {
                    System.out.println("Error! Please enter an integer greater than -100");
                }

                else if (i > max)
                {
                    System.out.println("Error! Please enter an integer less than 100");
                }

                else
                    isValid = true;
        }

        else 
        System.out.println("Error! Invalid number value");
        sc.nextLine();

    }
        // return the int
        return i;

}

public double getDoubleWithinRange(String prompt, double min, double max)
{

    int d = 0 ;
    boolean isValid = false;
    while (isValid == false)
    {
        System.out.println(prompt);
        if (sc.hasNextInt())
        {
            //if user chooses menu option less than 1 the program will print an error message
            d = sc.nextInt();
                if (d < min)
                {
                    System.out.println("Error! Please select menu option 1, 2, or 3");
                }
                //if the user chooses a menu option greater than 3 the program will print an error
                else if (d > max)
                {
                    System.out.println("Error! Please select menu option 1, 2, or 3");
                }
                //if the option is between 1 and 3 the menu option is valid
                else
                    isValid = true;
        }

        else 
        System.out.println("Error! Invalid number value");
        sc.nextLine();

    }
        // return the int
        return d;

}


public String getRequiredString(String prompt)
{
    return prompt;

}

public String getChoiceString(String prompt, String s1, String s2)
{
    return s2;

}

public int getInt(String prompt)
{
    return 0;

}

}

当我运行这个时,我继续得到我的最后一次打印,这是一个无效的数字值。我没有正确地从其他控制台的main方法导入代码吗?

1 个答案:

答案 0 :(得分:2)

这不是一个导入问题。如果您导入错误的内容,您的程序将首先进行编译,或者至少无法启动。

就实际解决问题而言,我建议您查看认为else中最外层getIntWithinRange相关联的两行,并考虑哪一行代码实际上与else相关联,而代码不是。{主要问题是ifelse只与一行相关联,除非您用大括号括起多行。因此,您应该将else的{​​{1}}修改为

getIntWithinRange

编辑:回复更新的问题

  

我在i下得到了一条波浪线,告诉我我没有使用局部变量,所以我不确定在这种情况下究竟是什么解决了,因为我试图在另一个类中使用它。我需要创建一个对象吗?

目前,您编写的代码并未在else { System.out.println("Error! Invalid number value"); sc.nextLine(); } 中使用i。这包括不将它传递给任何其他方法或任何构造函数。由于main是一个局部变量,因此i之外无法访问它,因此Eclipse会向您发出警告。这条&#34;波浪形的红线&#34; Eclipse的警告不会让您的代码停止编译和运行,但它可以帮助您在自己的代码中找到错误。

从更广泛的角度来看,由于未使用的本地化,会出现很多错误,Eclipse希望帮助您防止这些错误。考虑想要将二维数组的所有元素初始化为某个用户提供的数字的示例(变量名称比我在实际程序中使用的要短,但它们仍然可以得到重点):

main

Eclipse标记未使用的本地文件可以帮助您更轻松地找到这些类型的错误。

相关问题