Java中的随机整数错误

Java中的随机整数错误,java,random,Java,Random,这里的新手,我正在制作一个程序,生成一个带有适当后缀的随机序数 即第一、第二、第三。。。我不能得到随机数;工作。我不断发现以下错误: .\Random.java:6: error: class RandomInteger is public, should be declared in a file named RandomInteger.java public final class RandomInteger { ^ RandomNumSuf

这里的新手,我正在制作一个程序,生成一个带有适当后缀的随机序数 即第一、第二、第三。。。我不能得到随机数;工作。我不断发现以下错误:

    .\Random.java:6: error: class RandomInteger is public, should be declared in a  
file named RandomInteger.java  
public final class RandomInteger {  
             ^  
RandomNumSuffix.java:8: error: cannot access Random  
    Random rand = new Random();  
    ^  
  bad source file: .\Random.java  
    file does not contain class Random  
    Please remove or make sure it appears in the correct subdirectory of the  
sourcepath.
我在谷歌上搜索过,并没有找到堆栈溢出的解决方案。我甚至还抄过 并从互联网上编译了其他使用randomInt的程序;它们都会产生相同的错误。你能告诉我我做错了什么吗?以下是我的程序代码:

import java.util.*;

class RandNumSuffix
{
    public static void main(String [] args)
    {
        Scanner scan = new Scanner(System.in);
        Random rand  = new Random();
        String numSuffix = "";
        String answer = "";
        String repeat = "";
        int x;

        while(repeat.equalsIgnoreCase("yes")||repeat.equalsIgnoreCase("y" ))
        {
            x = rand.nextInt(1000000)+1;
            if (x == 1)
            {
                numSuffix = "st";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            if (x == 2)
            {
                numSuffix = "nd";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            if (x == 3)
            {
                numSuffix = "rd";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            if (x > 20 && x < 101 && x%10==1)
            {
                numSuffix = "st";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            if (x > 20 && x < 101 && x%10==2)
            {
                numSuffix = "nd";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            if (x > 20 && x < 101 && x%10==3)
            {
                numSuffix = "rd";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            if (x>100 && x%10==1 && x%100!=11)
            {
                numSuffix = "st";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            if (x>100 && x%10==2 && x%100!=12)
            {
                numSuffix = "nd";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            if (x>100 && x%10==3 && x%100!=13)
            {
                numSuffix = "rd";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            else
            {
                if (answer == "")
                {
                    numSuffix = "th";
                    System.out.print(x + numSuffix + ", ");
                }
            }


        answer = "";
        System.out.println("Would you like to generate another number?");
        answer = scan.nextLine();
        }
    }
} 
欢迎对我的节目提出批评。谢谢。

您的Random.java文件应命名为RandomInteger.java。重命名该文件,然后重试编译。它应该很好用

将类声明为公共类XYZ时,包含该代码的文件名必须命名为XYZ.java


希望这有帮助

错误:类RandomInteger是公共的,应该在名为RandomInteger.java的文件中声明。这说明了一切。文件名和公共类名在您的问题中应该匹配太多不一致:文件Random.java,RandomInteger类,但发布的代码源是指RandNumSuffix类?请花点时间正确地清理一切…我没有random.java文件,可能这就是我的问题。我认为Random是java.util包的一部分。这是否意味着每次我想要生成一个随机数时,我都需要为它创建一个类?不,不需要。