我是新來的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
謝謝你,這工作完美。另外,我很抱歉沒有鏈接到上一個問題。 – user1699107
爲什麼這會降低投票率? – Vulcan