我可以在java中获取循环内初始化的变量值并在循环外使用它吗?

问题描述 投票:0回答:1

我正在尝试构建一个剪刀石头布游戏。在本例中,我试图获取在 while 循环内初始化的变量的值,在本例中为整数变量 w 和 l。但出现变量未初始化的错误。


import java.util.Random;
import java.util.Scanner;
public class HEHE_19_Rock_Paper {
    public static void main(String[] args) {
        int t = 0;
        int w,l;

        while (t <= 3) {
// Player Logic of Picking
            Scanner s = new Scanner(System.in);
            System.out.println("Enter 'S' for Scissors, 'R' for Rock and 'P' for Paper");
            String i = s.next();
            int input;
            i = i.toLowerCase();
            switch (i) {
                case "s" -> input = 1;
                case "r" -> input = 2;
                case "p" -> input = 3;
                default ->
                        throw new IllegalStateException("Unexpected value: " + i + "Only 3 inputs are available 'S','R' and 'P'."); // suggested by Intelli J
            }
            if (input == 1) {
                System.out.println("You Have Picked Scissors!!!");
            } else if (input == 2) {
                System.out.println("You Have Picked Rock!!!");
            } else if (input == 3) {
                System.out.println("You Have Picked Paper!!!");

            } else {
                System.out.println("What are you doing!!! Only 3 inputs are available 'S','R' and 'P'.");
            }
            System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
// Computer Logic of Picking

            Random rand = new Random();
            int response = rand.nextInt(1, 4);

            if (response == 1) {
                System.out.println("PC Has Picked Scissors!!!");
            } else if (response == 2) {
                System.out.println("PC Has Picked Rock!!!");
            } else if (response == 3) {
                System.out.println("PC Has Picked Paper!!!");
            }

            System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            w = 0;
            l = 0;
// Deciding Logic
            if (input == response) {
                System.out.println("===========The Game is Draw!!!===========");
                l++;
            } else if (input == 1 && response == 2) {
                System.out.println("==========PC has won the Game!!==========");
                l++;
            } else if (input == 2 && response == 3) {
                System.out.println("==========PC has won the Game!!==========");
                l++;
            } else if (input == 3 && response == 1) {
                System.out.println("==========PC has won the Game!!==========");
                w++;
            } else if (response == 1 && input == 2) {
                System.out.println("==========You have won the Game!!==========");
                w++;
            } else if (response == 2 && input == 3) {
                System.out.println("==========You have won the Game!!==========");
                w++;
            } else if (response == 3 && input == 1) {
                System.out.println("==========You have won the Game!!==========");
                w++;
            }
            t++;
        
        }
        switch(w > l){
            case true -> System.out.println("You have won the game!!");
            case false -> System.out.println("You have lost the game!!");
            default -> System.out.println("The game is Draw");
        }
    }
}

我尝试在循环外部引入变量,因为我只想要变量内部的值,但我没有工作。

java intellij-idea while-loop
1个回答
1
投票

在 Java 中,在循环(或任何块)内声明的变量只能在该块内访问。如果要在 while 循环外使用 w 和 l 的值,则需要在循环外声明它们并在使用它们之前初始化它们。 这是例子

 public static void main(String[] args) {
        int t = 0;
        int w = 0; // Initialize w outside the loop
        int l = 0; // Initialize l outside the loop

        while (t < 3) { // Changed t <= 3 to t < 3 to play 3 rounds
© www.soinside.com 2019 - 2024. All rights reserved.