使用未解析的字符串打印

时间:2013-12-18 04:28:40

标签: java string variables println

当我尝试打印字符串时,我遇到了问题。我希望整个println读取类似“BAR,ORANGE和PLUM”的内容。这些字符串似乎与println语句没有通信。我是java的新手,我非常迷失所以请基本解释你的建议。我不仅非常感谢答案,而且还要解释,这样我就不会再犯这个错误了。谢谢你的时间。

/*
 * 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 -");       
        }
    }

}

3 个答案:

答案 0 :(得分:3)

你必须在类上声明你的变量。你现在的方式是,在方法中声明新的变量,它们只在那些方法的范围内。

public class SlotMachine_V2 extends GraphicsProgram {

    String aa;
    String bb;
    String cc;

    ...

}

然后你使用它们:

public void Wheel_1(){

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

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

        aa = " BAR, ";

    ...
}

答案 1 :(得分:2)

您在代码块中声明String aa =(" BAR, ");,因此可见性仅限于代码块。

尝试将String aa移至班级,即RandomGenerator rgen = new RandomGenerator();以下就可以了。

答案 2 :(得分:1)

只需使用

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

并且不要在aa,bb,cc

周围添加任何逗号
相关问题