Java - 可变范围

时间:2014-01-15 20:44:07

标签: java

我是java的新手,我有一个超级难以回答的问题。 (我确实有一些通用的编程知识)。我试图访问变量" item"无济于事。有人能说出原因吗?

    public void start() 
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter saleman's name: ");
        String name = input.next();

       int exit = 0;
       do 
       {
           System.out.println("Enter item number: ");
           String item = input.next();
           if (ValidateItem(item) == true)
           {
               if (Integer.parseInt(item) <=4 && Integer.parseInt(item) >=1)\
               {
                   exit = 1;
               }
               else 
                   System.out.println("Enter an item number between 1 and 4");
           }

           if (ValidateItem(item) == false)
           {
                System.out.println("Enter an item number between 1 and 4");
           }

       } while (exit == 0);

       int exitQuan = 0;
       do 
       {
           System.out.println("Enter quantity (1-99): ");
           String quant = input.next();
           if (ValidateItem(quant) == true)
           {
                exitQuan = 1;
           }
           else 
               System.out.println("Enter a quantity between 1 and 99");
       } 
       while (exitQuan == 0);

       if (item == 1) 
       {
           pay = 239.99;
       }

最后的IF声明是我缺乏范围的地方。 谢谢。

8 个答案:

答案 0 :(得分:3)

变量范围仅扩展到声明周围的最小一对括号。例如:

//this could be a method body, an if statement, a loop, whatever
{
    int x;
} //x passes out of scope here

因此,当您在item循环内声明do-while时,一旦退出循环,其范围就会结束。要解决此问题,请在循环上方声明item,如下所示:

String item = null; //initialize to null to avoid warning about using an un-initialized variable

do {
    System.out.println("Enter item number: ");
    item = input.next();

    //rest of loop...

} while (exit == 0);

这种方式item将一直有效,直到该方法返回。

答案 1 :(得分:1)

在do-while循环之外声明变量Item。

int exit = 0;
String item = null;
    do {
    System.out.println("Enter item number: ");
    itm = input.next();

答案 2 :(得分:0)

你已经在第一个do-while循环中声明了项目,你不能在第二个循环中使用它,因为当第一个循环中存在控制流时,item超出了范围。 / p>

另外,正如其他人所说,正确的意图对你有很大的帮助; - )

答案 3 :(得分:0)

item变量在第一个do while循环中声明 因此,您可以在do while循环中访问它 要访问函数范围内的项,您应该在do while循环之前声明它,以便可以在if(item == 1)语句中访问它

答案 4 :(得分:0)

int exit = 0;
String item = null;
do {
System.out.println("Enter item number: ");
item = input.next();
if (ValidateItem(item) == true){
    if (Integer.parseInt(item) <=4 && Integer.parseInt(item) >=1){
        exit = 1;}
else System.out.println("Enter an item number between 1 and 4");
}
if (ValidateItem(item) == false){
    System.out.println("Enter an item number between 1 and 4");}
} while (exit == 0);

int exitQuan = 0;
do {
 System.out.println("Enter quantity (1-99): ");
 String quant = input.next();
 if (ValidateItem(quant) == true){
            exitQuan = 1;}
    else System.out.println("Enter a quantity between 1 and 99");
} while (exitQuan == 0);

答案 5 :(得分:0)

您的变量item具有其定义的do while循环的范围。

int exit = 0;
String item = null; // <-- Add this

然后改变你的循环

do {
    System.out.println("Enter item number: ");
    item = input.next(); // <-- Defined outside this scope now.
    if (ValidateItem(item) == true) {
        if (Integer.parseInt(item) <= 4 && Integer.parseInt(item) >= 1) {
            exit = 1;
        } else {
            System.out.println("Enter an item number between 1 and 4");
        }
    } else { // <-- just use else
        // if (ValidateItem(item) == false) {
        System.out.println("Enter an item number between 1 and 4");
    }
} while (exit == 0);

答案 6 :(得分:0)

在Java中,变量是局部的(在方法内部或在构造函数内部声明)或实例变量(在类中但在方法之外声明)。在你的情况下,项目只存在于第一次执行时,你不能在循环外访问它。在do-while(全局变量)之外进行项目声明。我建议在Java中使用方法(我们在OOP中不是在程序范例中)。 祝你好运!

答案 7 :(得分:0)

如果他知道基本的java范围规则

,就不会遇到任何问题

1-参数声明的范围是声明出现的方法的主体

2-局部变量声明的范围是从声明出现的那一刻到该块的结尾

3- for语句标题的初始化部分中出现的局部变量声明的范围是for语句的主体和标题中的其他表达式

4-方法或领域的范围是整个班级

注意声明与类实例变量同名的局部变量,因为它会影响实例变量并使变量范围和程序可读性混淆