【问题标题】:How to get the hundreds place value from an integer value?如何从整数值中获取百位值?
【发布时间】:2016-12-05 13:46:20
【问题描述】:

给定 Java 中的 int,我希望能够确定数字的百位中的数字。例如:

  • 124 的输入应该给出 1 的输出
  • 357 的输入应该给出 3 的输出,
  • 653 的输入应该给出 6 的输出。

我尝试了下面的代码,但收到以下错误:

操作符 / 未定义参数类型 boolean, int

这是我的代码:

import java.util.Random;
class adam{
  public static void main(String[]args)
  {
    Random rnd = new Random();
    int first =   rnd.nextInt(900)+100;
    int second =  rnd.nextInt(900)+100;
    int third =  rnd.nextInt(900)+100;

    System.out.println(first);
    System.out.println(second);
    System.out.println(third);

    if ((first==second&&first==third)/100);
    System.out.println(first);
    System.out.println(second);
    System.out.println(third);
  }            
}

问题

  • 如何从 Java int 中获取百位值中的数字?
  • 为什么我的代码中出现此错误?

【问题讨论】:

  • 不知道你在问什么,但你的 if 语句由 boolean/100 组成,这不起作用。
  • int ans = 124/100 会给你 1
  • 这条线应该做什么? if ((first==second&&first==third)/100);
  • 记住你放在if的括号里的只是条件,然后在if之后,通常在大括号里,命令( s) 如果条件为真,它实际上必须运行。
  • 我将您的问题描述移至帖子顶部,并澄清了您预期的输入和输出。使用代码之前的上下文通常更容易理解问题。我还更新了您的标题以更准确地反映您的问题。请尝试尽可能清楚地提出问题。 How to Ask 页面有一些很好的提示。祝你好运!

标签: java if-statement int


【解决方案1】:

让我猜猜,你可能想要一些类似的东西:

if (first / 100 == second / 100 && first / 100 == third / 100) {
    System.out.println(first / 100);
    System.out.println(second / 100);
    System.out.println(third / 100);
}

如果条件为真,这将打印 3 次相同的值,但这可能不是您想要的。例如,如果数字是 756、723 和 792,它将打印 3 次 7。反正你可以试试看,看能不能修改成你的需要,如果不行,说明我猜错的地方。

编辑:要减少除数,您也可以在 if 语句之前将所有值除以 100:

first /= 100; // or if you prefer: first = first / 100;
second /= 100;
third /= 100;
if (first == second && first == third) {
    System.out.println(first);
    System.out.println(second);
    System.out.println(third);
}

【讨论】:

  • 这正是我想要做的,谢谢!我只是对 if (...) 中的内容感到困惑
【解决方案2】:

如何从 Java int 中获取百位值中的数字?

你的整数除法是正确的。您还可以使用模运算符来处理 >= 1000 的数字。

最简单的只是显示数学,这应该足以让您继续:

2468                       (1) start with an integer, 2468
2468 / 100 = 24            (2) divide by 100 to discard 10's and 1's
             24 % 10 = 4   (3) modulo 10 to discard 1000's and higher
                       4   (4) and your left with your 100's digit

如您所见,如果您知道不会看到 >= 1000 的数字,则可以在 (2) 处停下来。顺便说一句,如果您必须处理负数,则需要先取绝对值.

为什么我的代码中出现此错误?

因为这种说法是无稽之谈:

if ((first==second&&first==third)/100);

特别是条件是:

(first==second&&first==third)/100

如果你把它分成几个部分,你可以看到发生了什么:

first == second              (1) == produces a boolean
first == third               (2) == produces a boolean
(1) && (2)                   (3) && produces a boolean
100                          (4) 100 is an int
(3) / (4)                    (5) now you have boolean / int

而且您不能将boolean 除以int

【讨论】:

    【解决方案3】:
    1. 如何从 Java int 中获取百位值中的数字?

    此代码有效:

    import java.util.Random;
     class Adam{
      public static void main(String[]args)
      {
    Random rnd = new Random();
    int first =   rnd.nextInt(900)+100;
    int second =  rnd.nextInt(900)+100;
    int third =  rnd.nextInt(900)+100;
    
    System.out.println(first);
    System.out.println(second);
    System.out.println(third);
     int first1=first/100;
    int second1=second/100;
     int third1=third/100;
       System.out.println("an input of "+first +"  an output of "+first1 +" first");
    System.out.println("an input of "+second +" an output of "+second1+" first");
    System.out.println("an input of "+third +" an output of "+third1+" first");
       }            
    }
    
    1. 为什么我的代码中出现此错误? --->布尔值不能被 int 分割

    【讨论】:

      【解决方案4】:

      您只需要数字中的倒数第三位即可。 所以你可以试试这个:

      String temp = number + "";
      System.out.println(temp.charAt(temp.length() - 3));
      

      【讨论】:

      • OP 最能说明问题。我的印象是你在回答一个不同的问题,即数字 100 在三个随机数中出现了多少次。我的解释是 OP 在 100 位的数字之后,例如,如果数字是 690,则为 6。
      • 请在您的答案中包含解释,而不仅仅是代码:How to Answer
      • 当然我只是添加了解释。
      猜你喜欢
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 2012-04-27
      • 2012-10-04
      相关资源
      最近更新 更多