已经声明时找不到符号

时间:2017-12-08 15:34:24

标签: java

请有人帮我解决此错误:

import java.util.*;
class Cinema
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int cat;
        System.out.println("Choose your category : \n 1)Premium - Rs.150 \n 2)Gold - Rs.200 \n 3)Business Class - Rs.400");
        cat = sc.nextInt();
        switch(cat)
        {
            case 1:
            {
                System.out.println("You have selected Premium");
                int t = 150;   /* VARIABLE DECLARED */
                break;
            }
            case 2:
            {
                System.out.println("You have selected Gold");
                int t = 200;  /* VARIABLE DECLARED */
                break;
            }
            case 3:
            {
                System.out.println("You have selected Business Class");
                int t = 400;  /* VARIABLE DECLARED */
                break;
            }
            default:
            System.out.println("Invalid Option");
            break;
        }
        Scanner num = new Scanner(System.in);
        int n, amt;
        System.out.println("Choose number of tickets");
        n = num.nextInt();
        amt = t * n;      /* ERROR : cannot find symbol - variable t */
        System.out.println("You are buying " +n+ " tickets of " +cat+ " for Rs." +amt);
    }
}

我已经在一个case块中声明了变量t但它找不到它。 我查看了类似问题的许多答案,但我似乎无法解决它

4 个答案:

答案 0 :(得分:2)

您需要在切换块之前声明您的变量,以便可以在切换块之外访问它...请参阅下面的

    const adressValidator = (fg: FormGroup) => {
         test = fg.value; 

         if (!test.street || !test.city || ... ) {
             return { adress: { valid : false }};
         }
    }

    and 

   this.heroForm = this.fb.group({
      name: ['', Validators.required ],
      address: this.fb.group(new Address(), { validator: adressValidator}), // <-- How to make address required?
      power: '',
      sidekick: ''
    });

答案 1 :(得分:1)

您的't'变量在case块中重复声明,这会创建不同的变量。在这种情况下,只能在这些块内部看到它。您可以考虑在开关块之前放置变量声明。

答案 2 :(得分:0)

变量t被声明为里面上面代码中的switch语句导致错误。你需要在外面初始化它并相应地更改switch语句中的值

import java.util.*;

public class Main {

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int cat;
        int t=0;
        System.out.println("Choose your category : \n 1)Premium - Rs.150 \n 2)Gold - Rs.200 \n 3)Business Class - Rs.400");
        cat = sc.nextInt();
        switch(cat)
        {
            case 1:
            {
                System.out.println("You have selected Premium");
                t = 150;   /* VARIABLE DECLARED */
                break;
            }
            case 2:
            {
                System.out.println("You have selected Gold");
                t = 200;  /* VARIABLE DECLARED */
                break;
            }
            case 3:
            {
                System.out.println("You have selected Business Class");
                t = 400;  /* VARIABLE DECLARED */
                break;
            }
            default:
                System.out.println("Invalid Option");
                break;
        }
        Scanner num = new Scanner(System.in);
        int n, amt;
        System.out.println("Choose number of tickets");
        n = num.nextInt();
        amt = t * n;      /* ERROR : cannot find symbol - variable t */
        System.out.println("You are buying " +n+ " tickets of " +cat+ " for Rs." +amt);
    }

}

答案 3 :(得分:0)

好的,我想通了,我不得不把int t = 0;在切换块之前没有。 当我把它放在switch块之前时,它表示变量t已在main(Java.lang.String [])中定义。正确的代码如下:

import java.util.*;
class Cinema
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int cat;
        System.out.println("Choose your category : \n 1)Premium - Rs.150 \n 2)Gold - Rs.200 \n 3)Business Class - Rs.400");
        cat = sc.nextInt();
        switch(cat)
        {
            case 1:
            {
                System.out.println("You have selected Premium");
                int t = 150;   /* VARIABLE DECLARED */
                break;
            }
            case 2:
            {
                System.out.println("You have selected Gold");
                int t = 200;  /* VARIABLE DECLARED */
                break;
            }
            case 3:
            {
                System.out.println("You have selected Business Class");
                int t = 400;  /* VARIABLE DECLARED */
                break;
            }
            default:
            System.out.println("Invalid Option");
            break;
        }
        Scanner num = new Scanner(System.in);
        int n, amt;
        int t = 0;
        System.out.println("Choose number of tickets");
        n = num.nextInt();
        amt = t * n;      /* ERROR : cannot find symbol - variable t */
        System.out.println("You are buying " +n+ " tickets of " +cat+ " for Rs." +amt);
    }
}