有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    你这样做怎么样

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