简体   繁体   English

与ASCII Displayer有关的Dr.Java

[英]Dr.Java related to ASCII Displayer

I want to generate a sequence of numbers where: I want to compute the next number in the sequence by: (if the number is even, divide it by 2) and (if the number is odd multiply it by 3 and add 1). 我想生成一个数字序列,其中:我想通过以下方式计算序列中的下一个数字:(如果数字是偶数,则将其除以2)和(如果数字是奇数,则将其乘以3并加1)。

I am using ASCIIPrompter and I input the starting number which is 29. 我正在使用ASCIIPrompter,输入的起始编号为29。

The final display should repeatedly display the number in the sequence and generate the next number based on the conjecture above. 最终显示应按顺序重复显示数字,并根据上述猜想生成下一个数字。 The problem with my code is "num = even" gives me an error and i dont know how to fix this error. 我的代码的问题是“ num =偶数”给我一个错误,我不知道如何解决此错误。 HELP IS GREATLY APPRECIATED THANKS. 非常感谢您的帮助。

import java.awt.*; // for Color class
import static BasicIO.Formats.*; // for getCurrencyInstance, etc.
import static java.lang.Math.*; // for math constants and functions & random
import static java.awt.Color.*; // for Color constants (e.g. RED)

public class sequence {

    private ASCIIDisplayer display;
    private ASCIIPrompter prompt;

    public sequence() {

        display = new ASCIIDisplayer();
        prompt = new ASCIIPrompter();
        int num;
        prompt.setLabel("starting Number");
        num = prompt.readInt();
        for (int i = 1; i <= 5; i++) {
            if (num = even) {
                num = num / 2;
            } else {
                if (num = odd) {
                    num = num * 3 + 1;
                }
            }
            display.writeDouble(num);
        }
        display.close();
        prompt.close();

    }; // constructor

    public static void main(String[] args) {
        sequence c = new sequence();
    };
}

= is an asssignment =是一项转让

if (num = even) {

Thats infact assigning the (undefined?) value even to variable num. 那实际上分配了(undefined?)值甚至变量num。 Try this instead: 尝试以下方法:

 if (num % 2 == 0) {

And instead checking for odd in separate if you could simply use } else { - there is no third case for integers. 如果可以简单地使用} else { -则单独检查奇数,而不是整数的第三种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM