2012-09-26 35 views
2

我是新來的java,我遇到了另一個與我的骰子程序錯誤。(家庭作業)骰子程序,找不到符號

在這個程序中,我有三種方法,roll()來滾動模具並返回一個介於1和用戶指定的數字之間的值,getFaceValue()返回模具滾動的值作爲int, toString(),以字符串形式返回模具卷。

我之前關於我的程序的問題的答案使我能夠使程序正常工作,但它並不像教授想要的那樣結構化,所以我不得不重新修改它。我現在知道我的方法/變量都不是靜態的,我只能在程序的主要部分中接受用戶輸入。

當我嘗試編譯,我得到以下錯誤:

Die.java:12: error: cannot find symbol 
double y = (x * sides) + 1; 

symbol: variable sides 
location: class Die 

我的代碼如下:

import java.io.*; 
import java.util.*; 

public class Die { 
    //private int sides; 
    private int z; 
    private String faceName; 

    //sets (and returns) the face value to a uniform random number between 1 and the number of faces. 
    public int roll() { 
     double x = Math.random(); 
     double y = (x * sides) + 1; 
     z = (int)y; 
     return z; 
    } 

    //returns the current face value of the die. 
    public int getFaceValue() { 
     int face = z; 
     return face; 
    } 

    //returns the string representation of the face value. 
    public String toString() { 
     faceName = Integer.toString(z); 
     return faceName; 
    } 

    public static void main(String [] args) { 
     int sides; 
     System.out.println("How many sides will the die have?"); 
     Scanner keyboard = new Scanner(System.in); 
     sides = keyboard.nextInt(); 

     System.out.println(" "); 
     Die die = new Die(); 
     System.out.println("Roll: " + die.roll()); 
     System.out.println("Face: " + die.getFaceValue()); 
     System.out.println("String: " + die.toString()); 
    } 
} 

我本來變量雙方的私人詮釋(可以看到它在我的程序中註釋掉了),但是這產生了一個靜態引用錯誤。

我很感激您可以提供的任何幫助。

以前的問題是:Methods in Dice Program return 0

回答

1

可以爲sides創建一個實例變量,並通過構造函數將其傳遞給模具。這將需要您取消註釋您的private int sides變量。

例模具構造:

public Die(int sides) { 
    this.sides = sides; 
} 

有了這樣的構造函數,你可以用你邊讀作爲輸入的數量實例的模具。

sides = keyboard.nextInt(); 
Die die = new Die(sides); 

現在,你的模具的sides字段設置爲任何用戶輸入,和模具的方法,因此使用該值。

+0

謝謝你,這工作完美。另外,我很抱歉沒有鏈接到上一個問題。 – user1699107

+0

爲什麼這會降低投票率? – Vulcan

0

您尚未定義sidespublic int roll() {

範圍內創建一個變量中主縮小它的範圍只內main-您不能訪問的那之外除非你將它作爲一個變量傳遞(它將通過值傳遞),或者將其變爲一個類變量(一個字段)。

作爲一個死亡,你想讓它成爲實際課程中的一個領域。所以在類的頂部定義方面:

public class Die { 
    int sides; 

,並在構造函數爲它分配:

public Die(int s) { 
    sides = s; 
} 

然後當你打電話roll,身體兩側變量將可用。

+1

是的,這將工作,但認爲它像一個逼真的模型。如果你有一個骰子,它有多變的邊數,你可以隨時換掉它?不。 – Vulcan

+0

@Vulcan你說得對 - 我第一次寫答案時沒有考慮到這一點。更新以反映更好的課程整合。 –

0

您的變量兩側未定義 如果你能按值傳遞像這樣

public int roll(int sides) { 
    ..... 
    double y = (x * sides) + 1; 
    ..... 
} 
+0

是的,這會起作用,但想想它就像一個真實的模型。如果你有一個骰子,它有多變的邊數,你可以隨時換掉它?不。 – Vulcan

+0

@Vulcan,我經常把我的d6當作d2,d3或d6。 :p –

+0

@Vulcan在那裏好點,一個骰子構造函數會更合適,以便只設置一次邊......在賭博變化的骰子=獲得被踢出的獎金被踢出x) – MimiEAM

0

雙方作爲參數,以輥壓法。

import java.io.*; 
import java.util.*; 

public class Die { 
//private int sides; 
private int z; 
private String faceName; 

//sets (and returns) the face value to a uniform random number between 1 and the number of faces. 
public int roll(int sides) { 
    double x = Math.random(); 
    double y = (x * sides) + 1; 
    z = (int)y; 
    return z; 
} 


public static void main(String [] args) { 
     int sides; 
     System.out.println("How many sides will the die have?"); 
     Scanner keyboard = new Scanner(System.in); 
     sides = keyboard.nextInt(); 

     System.out.println(" "); 
     Die die = new Die(); 
     System.out.println("Roll: " + die.roll(sides)); // pass it as an argument to roll 
     System.out.println("Face: " + die.getFaceValue()); 
     System.out.println("String: " + die.toString()); 
    } 

    } 
+0

是的,這可以解決編譯錯誤,但是OP要求的解決方案與他的程序在其他問題(他不幸在此問題中沒有鏈接)中所做的一樣。 – Vulcan

0

您遇到的問題是sides對於main()方法是本地的,並且不會以任何方式傳遞給roll()方法。當編譯器正在編譯roll()時,它看到double y = (x * sides) + 1;,並且不知道sides指的是什麼 - 因此是錯誤。

有兩種方法可以完成此任務,但您應該找到一種方法將您從main()中的用戶收集的輸入傳遞給Die類。這樣做的一個方法是在模具類的構造函數:

public static void main(String [] args) { 
    int sides = keyboard.nextInt(); 
    ... 
    Die die = new Die(sides); 

之後再更改Die類,以便接受一個int並跟蹤它稱爲sides一個成員字段一個構造函數(儘管main()方法和Die類中變量的名稱不需要實際相同,但這只是一個方便)。

public class Die { 
    private int sides; 

    public Die(int sides) { 
     this.sides = sides; 
    } 

    public int roll() { 
     ... 
     double y = (x * this.sides) + 1;