【问题标题】:Use Random Value for Class Java and print it on Main对 Java 类使用随机值并在 Main 上打印
【发布时间】:2014-08-29 09:34:47
【问题描述】:

我需要对变量agentPosXagentPosY 使用随机值,我需要打印它们以查看它们得到的值。

代码很长,但我需要帮助的行是 52、53 和 322。

我用了static,但我觉得不对。

谢谢

import java.util.*;
import java.io.*;


public class WumpusWorld {
public static final int PIT_FLAG        = 1;
public static final int WUMPUS_FLAG     = 2;
public static final int GOLD_FLAG       = 4;
public static final int BREEZE_FLAG     = 8;
public static final int STENCH_FLAG     = 16;
public static final int GLITTER_FLAG    = 32;
public static final int BUMP_FLAG       = 64;
public static final int SCREAM_FLAG     = 128;

private static final int FACING_NORT    = 1;
private static final int FACING_EAST    = 2;
private static final int FACING_WEST    = 3;
private static final int FACING_SOUTH   = 4;

/*
 * Actions
 */
public static final int GO_FORWARD      = 1;
public static final int TURN_LEFT       = 2;
public static final int TURN_RIGHT      = 3;
public static final int GRAB            = 4;
public static final int SHOOT           = 5;
public static final int CLIMB           = 6;

public static final String STR_GO_FORWARD   = "Avanzar";
public static final String STR_TURN_LEFT    = "Izquierda";
public static final String STR_TURN_RIGHT   = "Derecha";
public static final String STR_GRAB         = "Agarrar";
public static final String STR_SHOOT        = "Disparar";
public static final String STR_CLIMB        = "Escalar";
//matriz random

/*
 * Perceptions
 */
private int     perceptions = 0;

public  double  actionCost  = 1.0;
public  double  outWithGold = 1000.0;
public  double  killCost    = 10000.0;

public  int     worldDimensionX;
public  int     worldDimensionY;
public  double  pitProbability;

public static  int  agentPosX   = (int)Math.random()*10;
public  static int  agentPosY   = (int)Math.random()*10;
private int     agentDir    = FACING_EAST;
private boolean agentSafe   = true;
private double  agentScore  = 0.0;
private boolean agentArrow  = true;
private boolean agentGold   = false;
private boolean agentOut    = false;

private int world[];

PrintStream out = System.out;
Scanner reader;



public static void main(String args[]) {
//WumpusWorld prin = new WumpusWorld(worldDimensionX,worldDimensionY, pitProbability, actionCost, outWithGold, killCost);
    int n = 100;
    if ( args.length == 1 ) {
        try {
            n = Integer.parseInt(args[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    System.out.println("SIMULATION_STARTED");
    System.out.println("gato: " + agentPosX + " gatoY: " + agentPosY);


    for (int i = 0; i < n; i++) {

        WumpusWorld world = new WumpusWorld(10, 
                            10, 
                            0.15,
                            1, 
                            1000, 
                            1000);

        System.out.println("EPISODE_STARTED");

        while ( world.isAgentOk() & !world.isAgentOut() ) {
            System.out.println( world.getPerceptions() );
            world.readAction();
        }
        System.out.println("EPISODE_ENDED, AGENT_SCORE=" + world.agentScore);
        String line = world.readLine();
        if ( !"START_SIMULATION_REQUEST".equals(line) ) break;
    }
}
}

【问题讨论】:

  • 322 是哪一行?您需要什么帮助?
  • 嗨 Elliot System.out.println("gato: " + agentPosX + " gatoY: " + agentPosY);我想打印 agentPosX 和 Y 从随机中获得的值。我在变量上使用了静态,但我一直得到 0 值,如果我不使用静态,我无法打印它们,如果这是一个愚蠢的问题,我很抱歉,但我无法让它工作。
  • 如果您删除所有多余的行,问题会更加清晰,包括正在打印的内容以及您期望打印的内容。提供行号并没有帮助,因为 stackoverflow 并不能让我们轻松查看行号。
  • 嗨,克里斯,我删除了所有多余的行
  • @user3816958,你可以走得更远一点。这个问题可以简化为只设置 agentPosX,然后询问为什么它总是被设置为零。并且您希望它是 1-9 的任何值(或者可能是 0-9,在这种情况下,我给出的答案会略有变化)。

标签: java variables random static main


【解决方案1】:

像这样转换Math.random() 的结果为时过早,您需要先进行乘法运算-

public static int agentPosX = (int) (Math.random()*10);
public static int agentPosY = (int) (Math.random()*10);

我建议你使用Random

private static Random rand = new Random();
public static int agentPosX = rand.nextInt(10) + 1;
public static int agentPosY = rand.nextInt(10) + 1;

【讨论】:

    【解决方案2】:

    这一行本质上就是您的代码中的内容。

        System.out.println( (int)Math.random()*10 );   // prints zero
    

    上面的代码总是打印零。为什么?因为您要将小于 1 的十进制值四舍五入为 int。这总是零。为了更清楚地看到这一点,请考虑以下示例:

        double v = Math.random();
    
        System.out.println( v );             // prints a decimal < 1
        System.out.println( (int) (v*10) );  // prints a value between 1 and 9
        System.out.println( (int) v*10 );    // prints 0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多