2014-01-15 88 views
0

我是全新的java,我有一个超级n00bish问题。 (我确实有一些一般的编程知识)。我试图访问变量“项目”无济于事。有人能找到原因吗?Java - 可变范围

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语句是我缺乏范围的地方。 谢谢。

+2

声明该变量的范围更大。现在它的范围是第一个'do-while'块。 –

+0

请根据Java编码约定格式化您的代码。 –

+0

你真的需要学习,如何格式化代码...另外,不要发布像半个方法。 – Vallentin

回答

1

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

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

你已经在第一do-while环路宣布项目,你不能在第二个使用它,因为当从第一循环中存在的控制流程,item超出范围。

而且,其他人则建议,适当intentation会帮助你很多;-)

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将一直持续到该方法返回。

0

项变量在第一DO宣布while循环 因此,你可以访问它的里面做的,而循环 要在功能的范围内访问的项目,你应该while循环之前做申报,所以这它可以在如果(项目== 1)语句

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); 
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); 
0

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

0

之一,如果他知道基本的Java范围规则

1-参数声明的范围是在该声明出现

2-范围的方法的主体不会面临任何问题本地变量声明的范围是从声明出现的位置到该块的末尾

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

4-方法或字段的范围是类的整个身体

注意有关声明具有相同的名称作为类的实例变量的局部变量,因为它会遮蔽实例变量,使有关范围的混乱的变量和程序可读性