【问题标题】:How to Check Whether a Variable is an Integer or Not如何检查变量是否为整数
【发布时间】:2015-02-25 08:50:26
【问题描述】:

我是 Java 编程新手,我正在尝试学习其中的技巧。我遇到了一个问题,问题源于我无法检查变量是否为整数。

int[] values = new int[3];
private Random rand = new Random();
num1 = rand.nextInt(70);
num2 = rand.nextInt(70);
num3 = rand.nextInt(70);

values[1] = num1 + num2

//we are dividing values[1] by num3 in order to produce an integer solution. For example (4 + 3) / 2 will render an integer answer, but (4 + 3) / 15 will not. 
//The (correct) answer is values[2]

values[2] = values[1]/num3

if (num3 > values[1]) {
     //This is done to ensure that we are able to divide by the number to get a solution (although this would break if values[1] was 1).
     num3 = 2
     while (values[2] does not contain an integer value) {
         increase num3 by 1
        work out new value of values[2]
        }    
} else {
    while (values[2] does not contain an integer value) {
         increase num3 by 1
        work out new value of values[2] <--- I'm not sure if this step is necessary
    }
}

System.out.println(values[2]);

【问题讨论】:

  • if (num == (int)num) { // 数字是整数 }
  • values 的定义位置和方式?
  • 这毫无意义。基元数组需要一个类型。他们只能“持有”这种类型。所以如果你有一个int[],所有的元素都是整数。

标签: java loops while-loop logic division


【解决方案1】:

另一种方式:

if(value%1 == 0){
  //True if Integer
}

例如2.34f % 1 将是 0.342f % 1 is 0.0

【讨论】:

    【解决方案2】:

    以下情况永远不会发生,因为values 数组是原始 int 类型。因此,检查values[2] 是否为 int 是事后的事。 values[]always 包含一个 int 并且尝试将一个非 int 类型的元素添加到 values 数组将导致 ArrayStoreException 被抛出。

    如果要分配的值的类型与组件类型不兼容(第 5.2 节),则抛出 ArrayStoreException。

    // All three are of type int
    num1 = rand.nextInt(70);
    num2 = rand.nextInt(70);
    num3 = rand.nextInt(70);
    
    // Not possible... 
    while (values[2] does not contain an integer value) {
    

    当您初始化 values 数组时,它将自动具有默认值 0

    int[] values = new int[3]
    System.out.println( Arrays.toString(values) );
    > [0,0,0]
    

    【讨论】:

      【解决方案3】:

      如果你用一个整数除以另一个整数,你总是有一个整数。

      你需要模(余数)运算

      int num1 = 15;
      int num2 = 16;
      int num3 = 5;
      
      System.out.println("num1/num3 = "+num1/num3);
      System.out.println("num2/num3 = "+num2/num3);
      System.out.println("num1%num3 = "+num1%num3);
      System.out.println("num2%num3 = "+num2%num3);
      

      如果模不为 0,则结​​果将不是整数。

      【讨论】:

        【解决方案4】:

        如果输入值可以是整数以外的数字形式,请检查

        if (x == (int)x){
           // Number is integer
        }
        

        如果正在传递字符串值,请使用 Integer.parseInt(string_var)。

        【讨论】:

          【解决方案5】:

          您可以使用 instanceof 运算符来检查给定对象是否为整数。

          if(num instanceof Integer)
              System.out.println("Number is an integer");
          

          【讨论】:

            【解决方案6】:
            if(value == (int) value)
            

            或长(64位整数)

            if(value == (long) value)
            

            或者可以安全地用浮点数表示而不会损失精度

            if(value == (float) value)
            

            如果传递的是字符串值,使用Integer.parseInt(value);如果传递的输入是正确的整数,它将返回一个整数。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2013-10-14
              • 2011-01-12
              • 2014-01-25
              • 1970-01-01
              • 2016-08-22
              • 2013-01-16
              • 2011-03-30
              相关资源
              最近更新 更多