java - Println 带有未解析的字符串

标签 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 -");       
        }
    }

}

最佳答案

您必须在类上声明变量。按照现在的方式,您在方法中声明新变量,并且它们仅在这些方法的范围内。

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, ";

    ...
}

关于java - Println 带有未解析的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20649632/

相关文章:

java - 尝试用 Java 解析 XML 文件并将变量存储在 ArrayList 中

java - 如何在一条 Java 语句中连接静态最终字符串数组

mysql - 在 shell 脚本中将表计数设置为变量..?

c# - C# 中超短且很棒的变量名

python - 只计算一次python变量

java - com.mysql.jdbc.driver 类未找到异常

java - HashTable 类的包含问题

java - 将 Tomcat 5.5 配置为 UTF-8 对所有 sendRedirect() 重定向进行编码?

python - 提取 pandas df 列中两个子字符串之间的字符串

ruby - 如何在 Ruby 中找到字符串中字符的索引?