我正在做一個班級任務,讓一枚硬幣翻轉並記錄它落在正面或反面的次數。我非常確定這其中的大部分都是正確的,但是拋棄我的部分是將這種方法稱爲「coinFlip」。當我嘗試在計數器類中調用它時,我收到錯誤消息「coinFlip無法解析爲類型」我似乎無法弄清楚如何解決該問題,或者瞭解爲什麼我會收到該消息。任何幫助表示讚賞,謝謝提前。調用方法的錯誤
package coinProject;
public class GenericCoin {
public class coinFlip{
private int heads = 0;
private int sideUp; //used to record which side the coin lands on
public coinFlip(){
flip();
}
public void flip(){
sideUp = (int) (Math.random() * 2);//used to keep random number under 2
}
public boolean headFlip(){
return (sideUp == heads);
}
public String toString(){//limits to only two print options, heads or tails
return (sideUp == heads) ? "Heads" : "Tails";
}
}
}
=========================================== =====================================
package coinProject;
public class counter {
public static void main(String[] args){
final int flip = 50;
int headFlips = 0 , tailFlips = 0;
coinFlip coin = new coinFlip();
for(int i = 1; i <= flip; i++){
coin.flip();
if(coin.headFlip()){
headFlips ++;
}
else{
tailFlips ++;
}
System.out.println(coin.toString());
}
System.out.println();//creates gaps after flips
System.out.println();
System.out.println("----- Flipped: " + flip);
System.out.println("----- Heads: " + headFlips);
System.out.println("----- Tails: " + tailFlips);
}
}
Java編碼風格傾向於以大寫開頭的類和包名。 另請參見:具有'coinFlip'作爲'GenericCoin'的內部類的具體原因? – Matthieu
coinFlip不是一種方法,它是一個內部類。 – Zarwan
這是一個意外,我添加了一個類,並在創建一個項目後開始工作 – Mrggg