简体   繁体   English

关于Java的一些基本问题

[英]Some basic questions about Java

I am new to Java.我是 Java 新手。 I know the basics and i have 2 questions that if they are answered they will really help me in programming.我知道基础知识,我有 2 个问题,如果他们得到回答,他们将真正帮助我进行编程。 Here's my code:这是我的代码:

package RollPack;

import java.util.Random;

public class RollClass {
     static int result;

    public static void main(String[] args){

        DerpRandom();


    }
    static void DerpRandom(){
        Random rand = new Random();
        result = rand.nextInt(7);
        System.out.println("You rolled a: "+result);
        DerpResult(result);
    }
    static void DerpResult(int result){
        if (result < 7){
            System.out.println("Yeh, it works..");
        }
    }
}

My questions:我的问题:

  1. What does DerpRandom(); DerpRandom();是什么? mean in words?是什么意思?
  2. What does DerpResult(result); DerpResult(result);是什么? mean in words?是什么意思?

I mean, by writing DerpResult(result) , does it mean that it adds result to DerpResult?我的意思是,通过编写DerpResult(result) ,是否意味着它将结果添加到 DerpResult 中? What does it mean?这是什么意思?

In words:用一句话来说:

DerpRandom(); = "Call the method named DerpRandom which has no parameters, and is in the current class". = "调用名为DerpRandom的方法,它没有参数,并且在当前类中"。

DerpResult(result); = "Call the method named DerpResult that takes one parameter assignment-compatible with int , and is in the current class, and pass it the value of result as an argument". = "调用名为DerpResult的方法,该方法采用一个与int兼容的参数赋值,并且在当前类中,并将result的值作为参数传递给它"。

When I say "in the current class" I mean "defined in the current class, or defined in one of its superclasses and accessible from the current class".当我说“在当前类中”时,我的意思是“在当前类中定义,或在其超类之一中定义并可从当前类访问”。

DerpRandom is a poorly named method which generates a random result between 0 and 6. DerpResult is a poorly named method which takes a parameter and ensures that the result comes in below 7. DerpRandom是一个命名DerpRandom方法,它生成一个介于 0 和 6 之间的随机结果。 DerpResult是一个命名DerpResult方法,它接受一个参数并确保结果低于 7。

No state is changed between the two methods.这两种方法之间没有状态改变。 DerpResult is only checking the value of DerpRandom . DerpResult只检查DerpRandom的值。

DerpRandom() means i have a function that i will call that takes no input and does some execution. DerpRandom() 意味着我有一个函数,我将调用它不接受输入并执行一些操作。 it returns nothing because its void它什么都不返回,因为它是空的

DerpRandom(result) means I have a function that takes an input of an integer called result and does something but returns nothing due to the void return type. DerpRandom(result) 表示我有一个函数,它接受一个名为 result 的整数的输入并执行某些操作,但由于 void 返回类型,什么都不返回。

What does the code do ?代码有什么作用?

derpRandom() prints a number from 0-6 then it calls DerpResult(result); derpRandom() 打印一个 0-6 的数字,然后调用 DerpResult(result);

derpResult(result) makes sure the number passed into it is less than 7 which it is when the other method called it. derpResult(result) 确保传递给它的数字小于 7,这是另一个方法调用它时的数字。

so you could call DerpResult(result);所以你可以调用DerpResult(result); there which is the same as calling DerpResult(6);那里与调用 DerpResult(6) 相同;

however you could also call DerpResult and change the input param, say DerpResult(1) which would yield the same thing or DerpResult(11) which wouldnt print anything.但是,您也可以调用 DerpResult 并更改输入参数,例如会产生相同结果的 DerpResult(1) 或不会打印任何内容的 DerpResult(11)。

DerpRandom and DerpResult both are class methods. DerpRandom 和 DerpResult 都是类方法。 By using the static keyword.通过使用静态关键字。 Which means these methods are created only one copy per class.这意味着这些方法每个类只创建一个副本。

DerpRandom is the caller method and DerpResult is the callee. DerpRandom 是调用者方法,DerpResult 是被调用者。

DerpRandom randomly selects random number, then prints the result with a message, and DerpResult method is called with a primitive type of int variable named result passed by argument. DerpRandom 随机选择随机数,然后将结果与消息一起打印出来,DerpResult 方法通过参数传递的名为result的原始类型 int 变量调用。 Which means the argument passed to DerpResult method is a duplicate copy.这意味着传递给 DerpResult 方法的参数是一个副本。 If for example the result was defined outside before the DerpResult is called and changed inside the method the change won't reflect after the DerpResult is called.例如,如果在调用 DerpResult 之前在外部定义结果并在方法内部更改,则在调用 DerpResult 之后更改不会反映。

The DerpResult method checks if the result is below 7 or not. DerpResult 方法检查结果是否低于 7。 If it is it prints "Yeh, it works.." Both of these methods has default access modifier or package access modifier which means the class where it is defined and all the java source code files in the same package can access these two methods.如果是,则打印“Yeh, it works..”这两种方法都有默认访问修饰符包访问修饰符,这意味着定义它的类和同一包中的所有 java 源代码文件都可以访问这两种方法。

Both of these methods don't return anything.这两种方法都不返回任何内容。 That's the reason why the void keyword is used.这就是使用void关键字的原因。

I mean, by writing DerpResult(result), does it mean that it adds result to DerpResult?我的意思是,通过编写 DerpResult(result),是否意味着它将结果添加到 DerpResult? What does it mean?这是什么意思?

No. It doesn't add anything.不,它没有添加任何东西。 DerpResult() method just compares the variable result with 7 to check if it is less than 7 or not. DerpResult() 方法只是将变量结果与 7 进行比较,以检查它是否小于 7。 If it is, it prints a message and if it is not it exits the method.如果是,则打印一条消息,如果不是,则退出该方法。 That's all.就这样。

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

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