【问题标题】:How to keep track of variables in if statements in java [duplicate]如何在java中的if语句中跟踪变量[重复]
【发布时间】:2021-05-18 02:49:34
【问题描述】:

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

我已经附上了下面的代码,所以你可以看到我在说什么,但我基本上需要关于变量 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!");
    }
        
   
  
}

}

【问题讨论】:

  • “继续跟踪变量”是什么意思?我的意思是,你想要跟踪什么?
  • answer == "yes" 不!这不是在 Java 中比较字符串的方法。
  • 这个程序有什么例外?请编辑您的问题以包含完整的堆栈跟踪。

标签: java if-statement variables


【解决方案1】:

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

 ...
    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;
....

【讨论】:

  • DV for if (answer == "yes")
  • 好的。但是,如果变量是在本地块中声明的,如何进一步比较变量呢?
  • 我认为您的回答正确,但请不要将错误的代码复制粘贴到您的答案中。请更正它,我将删除我的 DV 并给你并投票。
  • @ScaryWombat 您一直说不要将字符串与== 进行比较,并且您没有给人们提供解决方案。你可以说String 是一个对象,对于我们使用.equals(anotherObject) 来检查相等性的对象...
  • 我很抱歉@Renis1235
猜你喜欢
  • 2014-08-26
  • 1970-01-01
  • 2022-07-05
  • 1970-01-01
  • 1970-01-01
  • 2020-04-16
  • 2015-11-13
  • 2015-06-29
  • 1970-01-01
相关资源
最近更新 更多