当前位置: 首页 > 知识库问答 >
问题:

由于数据类型的原因,循环在计算之前退出

高正初
2023-03-14

我的程序规范如下。1.所有四位数都不同2.千位中的数字是十位中数字的三倍3.数字是奇数。4.数字之和是27。我遗漏了整个程序的一些代码。它有一个干净的编译,但当它运行时会自动终止。我认为问题出在数据类型的转换上。

int randomNumber = rand.nextInt(9000) + 1000;
String randomString;
boolean found = true;   

while (found)
{   

// converting to string to find position of digits and value        
randomString = String.valueOf(randomNumber);

// converting to char to transfer back to in while knowing the position of the digits 
char position0a = randomString.charAt(0);
char position1a = randomString.charAt(1);
char position2a = randomString.charAt(2);
char position3a = randomString.charAt(3);

// coverted back to int
int position0 = Character.getNumericValue(position0a);
int position1 = Character.getNumericValue(position1a);
int position2 = Character.getNumericValue(position2a);
int position3 = Character.getNumericValue(position3a);

int sumCheck = position0a + position1a + position2a + position3a;
int digit30Check = 3 * position2;

//checking addition to 27
String sumCheck27 = "27";
String sumCheck28 = String.valueOf(sumCheck);

// checking all digits are different
if (position0 != position1 && position0 != position2 && position0 != position3  &&

position1 != position2 && position1 != position3 && position2 != position3) 
{
if (position3 != digit30Check) // check if the digit in the thousands place 3 * tens
{
    if (sumCheck27.equals(sumCheck28)) // check if the sum is 27
    {
        if (position0 != 1 && position0 != 3 
&& position0 != 5 && position0 != 7 &&      
position0 != 9 && position1 != 1 && position1 != 3 
&& position1 != 5 && position1 != 7 && 
position1 != 9 && position2 != 2 && position2 != 3 
&& position2 != 5 && position2 != 7 && 
position2 != 9 && position3 != 3 && position3 != 3 && 
position3 != 5 && position3 != 7 && position3 != 9)
        { 
// checks for odd digits
         found = false;
         System.out.println(randomNumber);

        }
        else 
        randomNumber = rand.nextInt(9000) + 1000;
    }
    else 
    randomNumber = rand.nextInt(9000) + 1000;               
}
else 
randomNumber = rand.nextInt(9000) + 1000; 
}
else 
 randomNumber = rand.nextInt(9000) + 1000; 

 // end while
 }

共有3个答案

单凯捷
2023-03-14

我相信这可以通过观察给定的不等式来大大简化,比如

// thousands + hundreds + tens + ones == 27
// thousands = tens * 3, or tens = thousands / 3
// thousands = 3, 6 or 9
public static void main(String[] args) {
    // thousands = tens * 3; so it must be a multiple of 3.
    for (int thousands = 3; thousands < 10; thousands += 3) {
        for (int hundreds = 0; hundreds < 10; hundreds++) {
            if (hundreds == thousands) {
                continue;
            }
            // thousands = tens * 3; so the reverse is also true
            int tens = thousands / 3;
            if (tens == hundreds) {
                continue;
            }
            // All of the digits sum to 27.
            int ones = 27 - thousands - hundreds - tens;
            if (ones > 9 || ones % 2 != 0 || ones == tens
                    || ones == hundreds || ones == thousands) {
                continue;
            }
            int val = (thousands * 1000) + (hundreds * 100) + (tens * 10)
                    + ones;
            System.out.println(val);
        }
    }
}

当然,输出仍然是

9738
柯星华
2023-03-14

while(找到)交换为while(!找到)

逻辑地思考一下。你不想在找到东西的时候去看。你想在找不到的时候继续找。

洪飞鸿
2023-03-14
boolean found = false;  

while (found)

仅此一项就确保了while循环永远不会被输入,因为find是false。while循环中的任何内容都没有任何区别,因为它永远不会被执行。

你可能是想写

while (!found)

除了这个错误,你的情况也太复杂了。以下是简化它们的方法:

if ((position0 == (3 * position2)) && // note that position0 is the "thousands place", not position3
    ((position0+position1+position2+position3) == 27) && // sum of digits
    (position3 % 2 == 1) && // odd number
    (position0 != position1 && position0 != position2 && position0 != position3  &&
     position1 != position2 && position1 != position3 && position2 != position3)) { // different digits
    found = true;
}
 类似资料:
  • 问题内容: 我的程序规格如下。1.所有四个数字都不同。2.千位数字是十位数的三倍。3.数字是奇数。4.数字的总和是27。我遗漏了整个程序的一些代码。它具有干净的编译器,但运行时会自动终止。我认为问题出在数据类型的转换中。 问题答案: 仅此一项就确保了while循环将永远不会进入,因为它是false。while循环中的任何内容都没有任何区别,因为它将永远不会执行。 你可能想写 除了此错误外,您的情况

  • 我有一个扫描器,它由我的类中的调用。 循环的工作方式是,如果选择了“n”或“n”,它将再次调用构造函数。但是,由于这两种类型是字符串数据类型,构造函数首先调用一个名称,因此它要么将“n”或“n”识别为名称,要么跳过整个名称。我怎么解决这个? 下面的Player类 下面是以作为第一个名称输入的示例输出: 如您所见,问题出现在之后。

  • 我遵循了此处提出的计算循环数据平均值的建议: https://en.wikipedia.org/wiki/Mean_of_circular_quantities 但是我也想计算均方差。 上面给出了平均值,但我不知道如何计算SD 我试着只取正弦和余弦的标准偏差的平均值,但我得到了不同的答案。

  • 我已经在下面编写了简单的嵌套For循环; 在我的Android设备或模拟器中运行此操作,或的最大值应为和。 然而,代码似乎在max和max时退出(至少吐司消失了)。 对于较低的和值,它可以正常工作。我是不是遇到了内存问题或者发生了什么?

  • 我试图用while循环计算多个数字的和。当输入负数时,应打印数字之和。当我运行代码时,它所做的就是打印最后输入的正数。以下是当前的非工作代码:

  • 问题内容: 我是Kotlin的新手,正在玩数据类型。我选择了一个类型,然后尝试通过说来将其强制转换为a ,这在Java中是有效的(从语法上讲,但这是正确的)。但是,此操作失败,表示无法将Int强制转换为Double。我假设这是因为它是基于Integer类而不是原始的int数据类型构建的。我是正确的,最有价值的方法是什么?有一个功能,但这似乎效率低下且笨拙。 问题答案: 我花了一个类型,然后试图将它