2017-02-13 101 views
-4

這裏的新手,我正在製作一個程序,產生一個隨機的序數和正確的後綴
(I.E. 1st,2nd,3rd ...)我無法得到randomInt();上班。我不斷收到以下錯誤:在Java中的隨機整數錯誤

.\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. 

我在Google上搜索過和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(); 
     } 
    } 
} 

也歡迎對我的計劃的批評。謝謝。

+2

*錯誤:類RandomInteger是公共的,應該在名爲RandomInteger.java的 文件中聲明*這就是所有內容。文件名和公共類名應該匹配 –

+1

在你的問題中太多的不一致:文件Random.java,RandomInteger類,但是發佈的代碼源是指RandNumSuffix類?請花時間正確清理一切... –

回答

2

您的Random.java文件應命名爲RandomInteger.java。重命名該文件並重試編譯。它應該工作正常。

當您將類聲明爲public class XYZ時,包含該代碼的文件名必須命名爲XYZ.java


希望這有助於!

+0

我沒有一個random.java文件,也許這是我的問題。我認爲Random是java.util包的一部分。這是否意味着每次我想生成一個隨機數時,我都需要爲它創建一個類? – Turbowagon

+0

不需要。 – anacron