2016-11-09 110 views
0

我已經重新輸入LinkedIn學習材料Java fortune telling program.Radnom不能被解析爲一個類型 - 錯別字錯誤Java本身

很顯然,我沒有在隨機關鍵字錯字,但Java的顯示有關Radnom運行時錯誤。任何人都可以給我一個線索,可以導致這個錯誤?

錯誤:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

Radnom cannot be resolved to a type

Type mismatch: cannot convert from String to ArrayList

Syntax error, insert "Finally" to complete TryStatement at customPrograms.FortuneTeller.main(FortuneTeller.java:15)

圖片證明:

enter image description here

我的代碼:

package customPrograms; 
import java.util.Random; 
import java.util.Scanner; 
import java.util.ArrayList; 
import java.io.*; 

public class FortuneTeller { 

    public static void main(String[] args) { 
     File inputFile = new File("answers.txt"); 
     ArrayList<String> answers = new ArrayList<String>(); 
     String answer, response = "y"; 
     Random rand = new Random(); 
     System.out.println(rand); 
     try { 
      Scanner input = new Scanner(inputFile); 
      while(input.hasNextLine()) 
      { 
       answer = input.nextLine(); 
       answers.add(answer); 
      } 
     } catch(Exception e) { 
      System.out.println("The input file \"answers.txt\" was not found."); 
      System.out.println(e.toString()); 
     } 

     Scanner in = new Scanner(System.in); 
     while (response.equalsIgnoreCase("y")) 
     { 
      System.out.println("The fortune teller is ready for you \n" 
        + "Please think about question in your mind \n and hit enter for your reply"); 
     } 
     in.nextLine(); 
     System.out.println("The fortune teller says: \n" + answers.get(rand.nextInt(answers.size())) + 
       "\"\n"); 
     System.out.println("Do you have another question? (y/n)"); 
     response = in.nextLine(); 
    } 
} 
+0

'Radnom'是一個錯字嗎? – Berger

+1

您是否複製並粘貼了該代碼或者是否重新輸入了該代碼?重新訓練往往會讓問題神祕地消失。 – molbdnilo

+2

我認爲錯字只在Q ....的頭部或代碼不完整 –

回答

1

只要15號線是:

Random rand = new Random(); 

我想我想通了你的項目中發生了什麼:你沒有.class文件與.java源同步。


SOLUTION

項目菜單:

選擇構建項目選項。

之後標記自動構建保持文件同步。

enter image description here

+1

謝謝。它確實同步。我把它重新設置爲自動。 –

相關問題