gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:07:18 32 4
gpt4 key购买 nike

当我尝试打印字符串时遇到问题。我希望整个 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com