如何将一个真正的布尔值变成一个变量

how to make a true boolean into a variable

(这里是业余高中 Java 编码员)。我一直在从事这个项目,但遇到了一个我无法解决的问题。 以下是我需要帮助的内容: (我正在尝试编写一个程序来确定 driver 是否超速,如果是的话,三张罚单中的哪一张合适,以及从账户中扣除多少钱) 1.如何在if语句中使用reduc变量?当我像这样将它包含在最后一个 if 语句中时出现错误....

//variables
    int sbanka = 400;
    int speed = 60;
    int speedlimit = 60;
    int ticket1 = 100;

    //logic
    //Ticket 1
    if(speed > speedlimit + 10) {
        System.out.println("You will recieve the level 1 ticket");
        boolean reduc = true;
    }
        else {
            System.out.println("Your speed was fine.");
        }
            //bank account calculation for ticket 1
            if(reduc == true && sbanka >-100) {
                sbanka -= 100;
                System.out.println("we will deduct 100 dollars from your checking account");

这是完成的程序。感谢您的帮助。

package labs;

public class SpeedingTicket {

    public static void main(String[] args) {

    //variables   
    int speed = 50;
    int speedlimit = 60;
    String text = ("Your account balance is now ");
    boolean reduc = false;        

    //tickets
    int t1 = 100;
    int t2 = 150;
    int t3 = 200;

    //Bank account
    int sbanka = (400);


    //logic       
    if(speed > speedlimit + 10) {           
        System.out.println("You are getting a level one ticket");
        reduc = true;
    }    
        else {
            System.out.println("Your speed was fine.");
            reduc = false;                
        }       
            if(reduc == true) {
                System.out.println(text + (sbanka - t1));
            }


    if(speed > speedlimit + 20) {
        System.out.println("you are getting a level two ticket");
        reduc = true;
    }
        else {
            System.out.println("Your speed was fine.");
            reduc = false;
        }        
            if(reduc == true) {
                System.out.println(text + (sbanka - t2));
            }


    if(speed > speedlimit + 30) {
        System.out.println("This is a serious offence, you are getting a level 3 ticket");
        reduc = true;
    }
        else {
            System.out.println("Your speed was fine.");
            reduc = false;
        }         
            if(reduc == true) {
                System.out.println(text + (sbanka - t3));
            }

}

}

您在 if 语句 if(speed > speedlimit + 10){} => 局部变量下声明了 boolean reduc = true;。所以你不能在 if(){} 之外使用它。
很好读 http://www.geeksforgeeks.org/variable-scope-in-java/
重构如下:

//variables
    int sbanka = 400;
    int speed = 60;
    int speedlimit = 60;
    int ticket1 = 100;
    boolean reduc = false; // <= Fix your logic here by yourself

    //logic
    //Ticket 1
    if(speed > speedlimit + 10) {
        System.out.println("You will recieve the level 1 ticket");
        reduc = true;
    }
        else {
            System.out.println("Your speed was fine.");
        }
            //bank account calculation for ticket 1
            if(reduc == true && sbanka >-100) {
                sbanka -= 100;
                System.out.println("we will deduct 100 dollars from your checking account");