Pregunta

I am running into a problem when I try to println my strings. I want the entire println to read something like this "BAR, ORANGE, and PLUM". The strings don't seem to be communicating with the println statement. I am very new to java and I am extremely lost so please explain your suggestions very basically. I would really appreciate not only an answer, but also an explanation, that way I don't make this mistake again. Thank you for your time.

/*
 * This program will simulate playing a slot machine.
 * It will provide instructions to the user with an initial stake of $50 and then let the user play until either the money runs out or the player quits.
 * Author: Zac Saunders
 * Version: 1.3
 */


import acm.program.*;

import java.awt.*;

import acm.util.RandomGenerator;

public class SlotMachine_V2 extends GraphicsProgram {

    RandomGenerator rgen = new RandomGenerator();

    public void run(){

        waitForClick();
        rollWheels();
    }

    public void rollWheels(){

        Wheel_1();
        Wheel_2();
        Wheel_3();
        println("You rolled:"+aa, + bb, +cc"");
        run();

    }

    public void Wheel_1(){

        int a = rgen.nextInt(1, 6);

        if( a == 1){//BAR//

            String aa =(" BAR, ");

        }else if( a == 2){//BELL//

            String aa =(" BELL, ");

        }else if( a == 3){//PLUM//

            String aa =(" PLUM, "); 

        }else if( a == 4){//ORANGE//

            String aa =(" ORANGE, ");

        }else if( a == 5){//CHERRY//

            String aa =(" CHERRY, ");       

        }else if( a == 6){//DASH (-)//

            String aa =(" -, ");        
        }
    }

    public void Wheel_2(){

        int b = rgen.nextInt(1, 6);

        if( b == 1){//BAR//

            String bb =("BAR, ");

        }else if( b == 2){//BELL//

            String bb =("BELL, ");

        }else if( b == 3){//PLUM//

            String bb =("PLUM, ");  

        }else if( b == 4){//ORANGE//

            String bb =("ORANGE, ");

        }else if( b == 5){//CHERRY//

            String bb =("CHERRY, ");        

        }else if( b == 6){//DASH (-)//

            String bb =("-, ");     
        }

    }

    public void Wheel_3(){

        int c = rgen.nextInt(1, 6);

        if( c == 1){//BAR//

            String cc =("and BAR");

        }else if( c == 2){//BELL//

            String cc =("and BELL");

        }else if( c == 3){//PLUM//

            String cc =("and PLUM");    

        }else if( c == 4){//ORANGE//

            String cc =("and ORANGE");

        }else if( c == 5){//CHERRY//

            String cc =("and CHERRY");      

        }else if( c == 6){//DASH (-)//

            String cc =("and -");       
        }
    }

}
¿Fue útil?

Solución

You have to declare your variables on the class. The way you do it now, you declare new variables in the methods and they are in scope only in those methods.

public class SlotMachine_V2 extends GraphicsProgram {

    String aa;
    String bb;
    String cc;

    ...

}

Then you use them:

public void Wheel_1(){

    int a = rgen.nextInt(1, 6);

    if( a == 1){//BAR//

        aa = " BAR, ";

    ...
}

Otros consejos

You are declaring String aa =(" BAR, "); within code blocks so the visibility is limited to the code blocks.

try moving String aa to the class level i.e. just below RandomGenerator rgen = new RandomGenerator(); is fine.

just use

System.out.println(String.format("%s, %s, and %s",aa,bb,cc));

and don't put any commas around aa,bb,cc

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top