Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法在子类中无法正常工作,而在超类中却无法正常工作?_Java_Inheritance - Fatal编程技术网

Java 方法在子类中无法正常工作,而在超类中却无法正常工作?

Java 方法在子类中无法正常工作,而在超类中却无法正常工作?,java,inheritance,Java,Inheritance,我做了一个数字猜测程序,我试图把它输入到一个子类中。数字猜测程序在它自己的类中是成功的,但是当我运行我的类时,它不是最初的类,那么出于某种原因,相同的数字(0)是随机生成的吗?下面是超类代码: import java.util.*; public class GuessTheNumber { static int randomNum; static int guess; public static void main(String [] args) {

我做了一个数字猜测程序,我试图把它输入到一个子类中。数字猜测程序在它自己的类中是成功的,但是当我运行我的类时,它不是最初的类,那么出于某种原因,相同的数字(0)是随机生成的吗?下面是超类代码:

import java.util.*;

public class GuessTheNumber {

    static int randomNum;
    static int guess;

    public static void main(String [] args) {
        random();
        do {
            guess();
        }
        while (guess != randomNum);

    }

    public static void random() {
        // Math.Random() - 1s and 0s
        randomNum = (int) (Math.random() * 11);
    }

    public static void guess() {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a guess: ");
        guess = input.nextInt();

        System.out.println("Your guess is " + guess + ".");
        if (guess == randomNum)
            System.out.println("Your guess is correct! Yay!");
        else if (guess < randomNum)
            System.out.println("Guess higher.");
        else if (guess > randomNum)
            System.out.println("Guess lower.");
    }

}
import java.util.*;
公共类猜测编号{
静态int-randomNum;
静态整数猜测;
公共静态void main(字符串[]args){
随机();
做{
猜();
}
while(猜测!=randomNum);
}
公共静态void random(){
//Math.Random()-1s和0s
randomNum=(int)(Math.random()*11);
}
公共静态void guess(){
扫描仪输入=新扫描仪(System.in);
System.out.print(“输入猜测:”);
guess=input.nextInt();
System.out.println(“您的猜测是“+guess+”);
如果(猜测==随机数)
System.out.println(“你的猜测是正确的!耶!”);
else if(猜测<随机数)
System.out.println(“猜得更高”);
else if(猜测>随机数)
System.out.println(“猜测下限”);
}
}
下面是随机生成器不工作的子类的代码:

import java.util.*;

public class GuessTheNumber {

    static int randomNum;
    static int guess;

    public static void main(String [] args) {
        random();
        do {
            guess();
        }
        while (guess != randomNum);

    }

    public static void random() {
        // Math.Random() - 1s and 0s
        randomNum = (int) (Math.random() * 11);
    }

    public static void guess() {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a guess: ");
        guess = input.nextInt();

        System.out.println("Your guess is " + guess + ".");
        if (guess == randomNum)
            System.out.println("Your guess is correct! Yay!");
        else if (guess < randomNum)
            System.out.println("Guess higher.");
        else if (guess > randomNum)
            System.out.println("Guess lower.");
    }

}
import java.util.*;
公共类猜测编号{
静态int-randomNum;
静态整数猜测;
公共静态void main(字符串[]args){
随机();
做{
猜();
}
while(猜测!=randomNum);
}
公共静态void random(){
//Math.Random()-1s和0s
randomNum=(int)(Math.random()*11);
}
公共静态void guess(){
扫描仪输入=新扫描仪(System.in);
System.out.print(“输入猜测:”);
guess=input.nextInt();
System.out.println(“您的猜测是“+guess+”);
如果(猜测==随机数)
System.out.println(“你的猜测是正确的!耶!”);
else if(猜测<随机数)
System.out.println(“猜得更高”);
else if(猜测>随机数)
System.out.println(“猜测下限”);
}
}
我不确定我是否以某种方式改变了随机数生成器,使其不再工作,或者是因为我没有正确地执行将超类的方法放入子类的操作。
(如果术语/语法有任何错误,或者这个问题不清楚,我也很抱歉。显然已经晚了。谢谢。)

尝试使用
rand.nextInt()
而不是
randomNum=(int)(Math.random()
。也就是说

// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1)*11

,应该可以正常工作。

我们必须借助super关键字在子类中调用一个超类的方法,该方法的符号也相同。

我在你的代码中没有看到任何“子类”,我没有看到子类。另外,你正在使用
Math.random()
那么您的继承链会产生怎样的影响呢?据我所知,您发布了两次相同的代码。请再次查看您的问题。问题需要编辑,这一点很明显。如果您想编辑问题以符合OP的要求,请随意。如果OP说超类代码工作正常,而子类代码不正常,则他会发布只有超类,那个么你们怎么能对错误的问题提出建议或给出答案呢