该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
我要做个rolldice的小游戏,然后这是我的完整的code,我想直接把bank method 里面的那几个variable用在下面的checkwin method里面,但是总是显示找不到variable,应该是超过scope了。我想知道如何把改这个code。
import java.util.Random;
import java.util.Scanner;
public class roll{
public static void main(String[]args){
bank();
rollDice();
checkWin();
}
public static double rollDice(){
Random r = new Random();
for(int i = 1;i<=1;i++){
int number =r.nextInt(6)+1;
int number2 =r.nextInt(6)+1;
System.out.println("Dice rolls:"+number+" "+number2);
int sum = number+number2;
System.out.println("Your dice add up to:"+sum);
return sum;
}
}
public static void bank(){
Scanner i = new Scanner(System.in);
System.out.println("How much is in your bankroll?");
double money = i.nextInt();
System.out.println("Place your bet less than $"+money+"please");
double bet = i.nextInt();
System.out.println("Bet is: $"+bet);
System.out.println("Bankroll is: $"+money);
System.out.println("Bet has been laid");
}
public static boolean checkWin(){
if(sum == 7||sum ==11){
System.out.println("you win!!");
money= money + bet;
return true;
}else if (sum == 2||sum == 3||sum== 12){
System.out.println("you lose");
money =money - bet;
return false;
}else{
System.out.println("start again");
}
}
}