如何在java中跟踪if语句中的变量

如何在java中跟踪if语句中的变量,java,if-statement,variables,Java,If Statement,Variables,我无法跟踪随机数生成器分配的变量。我尝试生成两个不同范围的随机数,并根据更新分数或不更新分数生成的数字,然后根据一组规则告诉用户他们是赢还是输 我已经附上了下面的代码,所以你可以看到我在说什么,但我基本上需要变量roll1和roll2的帮助,谢谢你的帮助 import java.util.Random; // import random number class import java.util.Scanner; // Import the Scanner class public class

我无法跟踪随机数生成器分配的变量。我尝试生成两个不同范围的随机数,并根据更新分数或不更新分数生成的数字,然后根据一组规则告诉用户他们是赢还是输

我已经附上了下面的代码,所以你可以看到我在说什么,但我基本上需要变量roll1和roll2的帮助,谢谢你的帮助

import java.util.Random; // import random number class
import java.util.Scanner;  // Import the Scanner class
public class Main
{

public static void main(String[] args) {
    //rules and variables 
    System.out.println("Welcome to lucky dice, the computer will roll 2 dice...");
    //int roller1 = (int )(Math.random() * 10 + 1); //instance of random class
    //int roller2 = (int )(Math.random() * 20 + 2); //instance of random class
    //Random rand = new Random();
    int total = 0;
    int num = 0;
    int max = 11;
    int min = 1;
    int max2 = 21;
    int min2 = 2;
    
    //create a loop to keep rolling until the user wins or looses 
    while (num <= 20 && total >= -50 && total < 150){
        System.out.println("Are you ready to roll?");
        Scanner reply = new Scanner(System.in);  // Create a Scanner object
        String answer = reply.nextLine();
        Random rand = new Random();
        if (answer == "yes"){
            int roll1 = rand.nextInt(max - min) + min;//instance of random clas
            int roll2 = rand.nextInt(max2 - min2) + min2;//instance of random clas
           
            
        }while(roll2 % 2 == 1){
            int roll2 = rand.nextInt(max2 - min2); //instance of random clas
        }if (roll1 > roll2 || roll2 > roll1){
            total = total + roll1 + roll2; //keep track of score and tell user
            System.out.printf("Your roll was: " + roll1, roll2 + ".");
            System.out.printf("Your score is now " + total + ".");
            num = num + 1;
        }else if (roll1 == roll2){
            total = total + roll1 + roll2; //keep track of score and tell user
            System.out.printf("Your roll was: " + roll1, roll2 + ".");
            System.out.printf(" Your score is now " + total + ". ");
            num = num + 1;
        }
    }if (num > 20){
        System.out.println("You lost!");
    }else if (total >= 150){
        System.out.println("You won!");
    }else if (total >= -50){
        System.out.println("You lost!");
    }
        
   
  
}

}
import java.util.Random;//导入随机数类
导入java.util.Scanner;//导入Scanner类
公共班机
{
公共静态void main(字符串[]args){
//规则和变量
System.out.println(“欢迎使用幸运骰子,计算机将掷2个骰子…”);
//int roller1=(int)(Math.random()*10+1);//随机类的实例
//int roller2=(int)(Math.random()*20+2);//随机类的实例
//Random rand=新的Random();
int-total=0;
int num=0;
int max=11;
int min=1;
int max2=21;
int min2=2;
//创建一个循环,直到用户赢或输为止
而(数值=-50&&total<150){
System.out.println(“准备好开始了吗?”);
Scanner reply=new Scanner(System.in);//创建一个Scanner对象
字符串answer=reply.nextLine();
Random rand=新的Random();
如果(回答=“是”){
int roll1=rand.nextInt(max-min)+min;//随机类的实例
int roll2=rand.nextInt(max2-min2)+min2;//随机类的实例
}while(roll2%2==1){
int roll2=rand.nextInt(max2-min2);//随机类的实例
}如果(roll1>roll2 | | roll2>roll1){
total=total+roll1+roll2;//记录分数并告诉用户
System.out.printf(“您的卷是:+roll1,roll2+”);
System.out.printf(“您的分数现在是“+total+”);
num=num+1;
}else if(roll1==roll2){
total=total+roll1+roll2;//记录分数并告诉用户
System.out.printf(“您的卷是:+roll1,roll2+”);
System.out.printf(“您的分数现在是“+total+”);
num=num+1;
}
}如果(数量>20){
System.out.println(“你输了!”);
}否则,如果(总数>=150){
System.out.println(“你赢了!”);
}否则如果(总数>=-50){
System.out.println(“你输了!”);
}
}
}

您已经在if块中声明了一个变量。声明上面的变量级别

 ...
    String answer = reply.nextLine();
    Random rand = new Random();
        int roll1 = 0;
        int roll2 = 0;
    if (answer.equals("yes")){
        roll1 = rand.nextInt(max - min) + min;//instance of random clas
        roll2 = rand.nextInt(max2 - min2) + min2;//instance of random clas  
    }...
还是搬到那里去

...
int min = 1;
int max2 = 21;
int min2 = 2;
int roll1 = 0;
int roll2 = 0;
....

“保持跟踪变量”是什么意思?我的意思是,您想要跟踪什么?
answer==“yes”
No!这不是Java中比较字符串的方法。这个程序有什么例外?请编辑您的问题,将
if(答案==“yes”)
ok的完整堆栈trace.DV包括在内。但是,如果变量是在本地块中声明的,那么如何进一步比较它们呢?我认为您的回答是正确的,但请不要将错误的代码复制粘贴到您的答案中。请更正它,我将删除我的DV,并给你和upvote。@ScaryWombat你一直说不要将字符串与
==
进行比较,你也不会给人们一个解决方案。您只需知道
String
是一个对象,对于对象,我们使用
.equals(另一个对象)
来检查相等性……非常抱歉@Renis1235