如果导致变量无法解析,则在语句外定义变量会导致重复的局部变量?

时间:2016-05-10 15:51:57

标签: java

我试图让我的代码根据用户输入决定运行哪些位。我似乎无法使它工作,因为代码中找不到循环中的变量,并且在if语句之前定义它们会导致重复的变量错误。

有问题的部分

import java.util.Scanner;


public class test 
{

    public static void main(String[] args)
    {



        Scanner scan = new Scanner(System.in);

        System.out.println("Welcome to our version of Conways Game of Life!");
        System.out.println();

        System.out.print("Please enter the amount of Rows for the board: ");
        int ROWS = scan.nextInt();
        System.out.println();

        System.out.print("Please enter the amount of Columns: ");
        int COLS = scan.nextInt();
        System.out.println();

        System.out.println("Woud you like to pick a pattern or generate a random world?");
        System.out.print("Pick a pattern = 1  Random World = 2 : ");
        int choice = scan.nextInt();
        System.out.println();


        if (choice == 1)
        {
            System.out.println("Please enter the pattern # you want");
            System.out.print("Glider = 1 " + "Tencell = 2 " + "Exploder = 3 " 
            + "Tub = 4 : ");
            int p = scan.nextInt();
            System.out.println();

            System.out.print("What row do you want the pattern to start at?: ");
            int r = scan.nextInt();
            System.out.println();

            System.out.print("What column do you want the pattern to start at?: ");
            int c = scan.nextInt();
            System.out.println(); 
        }

            System.out.print("How many generations do you want to go through? ");
            int g = scan.nextInt();
            System.out.println();

            System.out.print("How many seconds do you want to wait in-between generations? ");
            int s = scan.nextInt();
            System.out.println();



        World test = new World (ROWS, COLS);

        test.getWorld();

        if (choice == 1)
        {
            test.putPattern(r,c,p);
        }
        else if (choice == 2)
        {
        test.random();
        }

        test.printWorld();

        System.out.println("Press enter to start generations!");
        new Scanner(System.in).nextLine();

        for(int i=0; i<g; i++)
        {
            System.out.println ("Generation " + (i+1)); 
            test.nextGen();
            System.out.println(" ");
            test.printWorld();
            System.out.println(" ");

            try 
            {
                Thread.sleep(s*1000);
            } catch (InterruptedException e) 
            {
                e.printStackTrace();
            }
        } 

    }

}

问题部分

if (choice == 1)
    {
        test.putPattern(r,c,p);
    }

变量r,c和p无法解析为我的更大代码中的变量。在顶部的if语句之前设置变量会导致重复变量错误。

1 个答案:

答案 0 :(得分:3)

你做这样的事情怎么样?

        int p = 0;
        int r = 0;
        int c = 0;
        if (choice == 1)
        {
            System.out.println("Please enter the pattern # you want");
            System.out.print("Glider = 1 " + "Tencell = 2 " + "Exploder = 3 " 
            + "Tub = 4 : ");
            p = scan.nextInt();
            System.out.println();

            System.out.print("What row do you want the pattern to start at?: ");
            r = scan.nextInt();
            System.out.println();

            System.out.print("What column do you want the pattern to start at?: ");
            c = scan.nextInt();
            System.out.println(); 
        }

然后:

    if (choice == 1)
    {
        // may be check if r,c,p are 0 and leave or process or whatever you want
        test.putPattern(r,c,p);
    }