我有一個問題,我需要一些建議:我需要編寫一個稱爲hasEight()
布爾方法,它需要一個int
作爲輸入,並且如果所述數目包含數字8返回true(例如,18,808)。如何查找數字是否包含某個數字? (使用Java)
我不想使用「字符串轉換方法」。
.........................................
這就是我做了什麼:
import java.util.Scanner;
public class Verificare {
public static boolean hasEight(int numarVerificat) {
int rest = numarVerificat % 10;
return rest == 8;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Introduceti numarul pentru verificare: ");
int numar = keyboard.nextInt();
Verificare.hasEight(numar);
System.out.println("Afirmatia este: " + Verificare.hasEight(numar));
keyboard.close();
}
}
這是功課?如果是這樣,您可能希望將其標記爲此,向我們展示您迄今爲止所嘗試的內容,並解釋您卡住的位置。 –
的可能的複製[如何獲得一個int數的獨立數字?](http://stackoverflow.com/questions/3389264/how-to-get-the-separate-digits-of-an-int-number) –
'808'是什麼意思?小數點右邊?所以它是'8x10^2 + 0x10^1 + 8x10^0'。所以;鑑於此,您將如何分別訪問每個數字? –