私は3つの変数にユーザー入力を読み込み、それが整数であるかどうかを判断するメソッドを呼び出したいと思っています。変数。私がコンパイルしようとすると、私は次のコンパイラエラーが表示されます。ここではメソッドでスキャナを使用できません
Error: The method getInt(int) in the type Methods is not applicable for the arguments (java.util.Scanner)
は私のコードです:
import java.util.Scanner;
public class Methods{
public static void main(String [] arg){
Scanner scan=new Scanner(System.in);
int a,b,c;
System.out.println("Enter three ints");
a = getInt(scan.nextInt());
b = getInt(scan.nextInt());
c = getInt(scan.nextInt());
}
public static int getInt(String input){
Scanner scan=new Scanner(System.in);
int num = 0;
while (num <1) {
if(scan.hasNextInt()) {
int number = scan.nextInt();
num +=1;
}
else{
System.out.println("Invalid input. Please enter an integer value.");
input.next();
}
}
}
あなたが与えることができる任意の助けをいただければ幸いです!
あなたの方法 'getInt'は' int'変数を取りますそのパラメータとして。 'int'が期待される場所に' Scanner'インスタンスを渡すことはできません。また、あなたのコードは与えられたようにコンパイルされません...関数 'getInt'の内部で、' input'(関数 'getInt'に' int'型のパラメータ)を 'Scanner'型に再定義しています。 –