“variable”不能解析为变量

whhtz7ly  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(387)

这个问题在这里已经有答案了

java,“变量名”不能解析为变量(3个答案)
上个月关门了。
我试图将第二个if语句放在loop语句和第一个if语句中,它一直说“exists”不能解析为变量(

import java.util.*;

public class MT_Assign2_Phonebook_LinkedList {

    public static void main(String[] args) {

        LinkedList <String> AddEntry_list = new LinkedList <String> (); //FirstName & Last Name
        LinkedList <String> Number_list = new LinkedList <String> (); //Telephone Number

        String firstname, lastname;
        String phonenum;

        Scanner input = new Scanner (System.in);

        System.out.println ("MAIN MENU");
        System.out.println ("1 = ADD Phonebook Entry");
        System.out.println ("2  = DELETE Phonebook Entry");
        System.out.println ("3 = VIEW All Entries ");
        System.out.println ("4 = SEARCH Entries ");
        System.out.println ("5 = Quit ");

        //ENTER USER'S CHOICE
        System.out.print ("\nPlease Select your choice: ");
            int choice = input.nextInt();

            if (choice == 1) {
                    System.out.println ();
                    System.out.println ("You chose to ADD PHONEBOOK ENTRY");

                    //.get (record the input data ni user) with VALIDATION
                        for (int database_pb = 0; database_pb <AddEntry_list.size (); database_pb++) {

                            if (AddEntry_list.get(database_pb).contains(firstname) && AddEntry_list.get(database_pb).contains(lastname))
                            {
                                System.out.println ("\n  ERROR OCCURED: PHONEBOOK ENTRY ALREADY EXIST!\n  END OF PROGRAM");
                                System.exit(0);

                                boolean exists = true; //validate

                            }//END IF

                            if (exists == false)    {

                                System.out.print ("First Name: ");
                                System.out.print ("Last Name: ");
                                System.out.print ("Number (+63): ");
                                AddEntry_list.add (firstname + " " + lastname + " " + phonenum + " ");
                                Number_list.add (phonenum + " " + firstname + " " + lastname + " " );

                                System.out.println ("Successfully Added Entry! ");

                            }  boolean exists =  false;
                        }//END OF FOR LOOP

            }//CHOICE 1

                else 
                    System.out.println ("Invalid Input!");

    } //public static

}//public class end
5lwkijsr

5lwkijsr1#

范围规则就是解释。
任何给定的变量都在某个地方声明。找到“相关”的大括号批( { 以及 } ). 您已经命令此变量存在于该范围内。没有别的地方。
“相关”批始终是最接近的包含批,参数除外,其中它是方法定义本身。很简单。
所以,你有这个街区:

{
  System.out.println ("\n  ERROR OCCURED: PHONEBOOK ENTRY ALREADY EXIST!\n  END OF PROGRAM");
  System.exit(0);
  boolean exists = true; //validate
}

那是你工作的地方 exists 变量存在(呵呵),其他地方都没有。
注意:那个代码块是胡说八道。退出程序时会显示一条错误消息,然后说:“exists=true”???那没道理。

相关问题