新手在这里,我正在制作一个程序,生成一个带有正确后缀的随机序号 (I.E. 1st,2nd,3rd ......)我不能得到randomInt();上班。我一直收到以下错误:
.\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.
我在谷歌和Stack Overflow上搜索了无法找到解决方案。我甚至复制了 并使用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();
}
}
}
欢迎批评我的节目。感谢。
答案 0 :(得分:2)
您的Random.java
文件应命名为RandomInteger.java
。重命名文件并再次尝试编译。它应该工作正常。
当您将类声明为public class XYZ
时,包含该代码的文件名必须命名为XYZ.java
。
希望这有帮助!