简体   繁体   English

使用if语句时,为什么我的代码没有给出正确的答案?

[英]Why does my code not put out the right answer when I use the if statement?

This is the class that contains main. 这是包含main的类。

import java.util.Random;

public class RandomTest {

    public static void main(String[] args) {
        String name;
        Random r = new Random();
        int number = 1 + r.nextInt(3);

        System.out.println(number);

        if (number == 1) {
            name = "Kobe";
        }
        else if (number == 2) {
            name = "Mamba";
        }
        else {
            name = "lol";
        }

        RandomTest2 object = new RandomTest2(name);
        System.out.println(object.toString());
    }
}

This class contains other methods. 此类包含其他方法。

public class RandomTest2 {

    private String name;

    public RandomTest2(String name) {
        name = name;
    }

    public String getName() {
        return name;
    }

    public String toString() {
        return getName();
    }
}

If I remove the If statement and I assign a value directly to name, it works.. 如果删除If语句并直接为name分配一个值,则它将起作用。

I just want to randomly assign properties to the object. 我只想向对象随机分配属性。

You just missed a this in your constructor: 您只是在构造函数中错过了一个this

public RandomTest2(String name) {
    this.name = name;
}

Without it, you are just assigning the name parameter to itself. 没有它,您只是将name参数分配给它自己。

No worries, this gets every Java coder at least once ;-) 不用担心,这会使每个Java编码器至少一次;-)

Instead of name = name as shown below 代替name = name如下所示

RandomTest2(String name) {
    name = name;
}

Try 尝试

this.name=name;

on this function: 关于此功能:

public RandomTest2(String name) {
    name = name;
}

the compiler is understand that you assign name to itself not for the name variable on RandomTest2. 编译器了解到,您不是为RandomTest2上的name变量分配名称,而是为其本身分配名称。 So change it to this.name = name. 因此将其更改为this.name = name.

类RandomTest2->构造方法-> this.name = name

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

相关问题 为什么在我可以登录之前计算器会自动回答“ 0”? - Why does my calculator automatically answer “0” before i can put my sign in? 为什么我的代码只有在我注释掉“break;”时才有效? - Why does my code only work when I comment out "break;"? 为什么当我在for循环中放入&lt;= 10时,它仅输出5? - why does it only output 5 when i put <=10 in my for loop? 试图为我的代码获得正确的价值答案 - Trying to get the right value answer for my code 当我使用文本文件时,为什么我的代码打印空数组([])? - Why does my code print empty array([]) when I use text files? 我不知道为什么我的代码给出了错误的答案 - I don't know why my code is giving wrong answer 当我只输入2个输入时,为什么else语句不起作用? - Why doesn't my else statement work when I put only 2 inputs? 当我输入整数时,为什么我的Java双变量类型变为0.0? - Why is my Java double variable type coming out to 0.0 when I put in an integer? 为什么我在运行程序时显示代码中的所有字母等级选项? 我只想要显示正确的答案 - Why are all the letter grade options in my code being shown when I run the program? I only want the correct answer shown 当我尝试在 Eclipse 中启动服务器时,为什么 Tomcat“内存不足”正确? - Why does Tomcat 'Out of Memory' right when I attempt to start server in Eclipse?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM