2012-03-28 58 views
0

我正在做作业,我很近,但我遇到问题。我刚刚学会了如何在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. 

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

我有一个名为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; 

} 

}

当我运行此我不断收到我的最后一次打印这是一个无效的数值。我是不是正确地从其他控制台的主要方法导入代码?

+1

与println(String s)和print(String s)没有任何关系。 – 2012-03-28 23:42:00

+0

@Mike Samuel我刚开始写代码,并给了我需要的一些规格,所以我跳进了它。不是最简洁的写法,但我想以最简单最难的顺序来处理每种方法。 – 2012-03-28 23:50:26

回答

2

这不是进口问题。如果你导入了错误的东西,你的程序不会首先编译,或者至少不会启动。

至于实际修理你的问题,我建议你看你认为与最elsegetIntWithinRange相关的两条线,并考虑其代码实际上与else和代码关联不大。主要问题是ifelse只与一条线关联,除非用花括号包围多条线。所以,你应该修改的getIntWithinRange最后else看起来像

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

编辑:为响应更新的问题

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

现在,您所写的代码不会在main中使用i。这包括不将它传递给任何其他方法或任何构造函数。由于i是本地变量,因此无法在main之外访问,所以Eclipse会警告您。 Eclipse提供的这种“扭曲的红线”警告不会阻止你的代码编译和运行,但它的目的是帮助你在自己的代码中发现错误。

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

public static final int N = 10; 

public void main(String[] args) { 
    int[][] a = new int[N][N]; 
    int n = /* Read number from the user */; 

    for (int i = 0; i < N; i++) { 
     for (int j = 0; j < N; j++) { 
      a[i][j] = i; // Bug - we meant to initialize to n 
     } 
    } 

    // ... 
} 

Eclipse对未使用的本地人进行标记可帮助您更轻松地找到这些类型的错误。

+0

谢谢!我认为它解决了它。我没有在我的代码中看到。我刚刚编辑了我在第一堂课注意到的问题(我发布的第一部分代码)。我得到的代码行的问题 int i = c 它告诉我我没有使用本地变量。我需要关心吗?我不确定该怎么做,因为我认为我是将这些信息带入其他班级。 – 2012-03-28 23:48:39

+0

@JeremyB我编辑了我的答案,以解释为什么Eclipse显示错误。一旦你通过将“i”传递给构造函数“将这些信息带入另一个类中”,那条红色的曲线就会消失。 – 2012-03-28 23:53:39

相关问题