import java.util.*;
public class decimalToBinaryTest {
public static void main(String[] args) {
int number;
Scanner in = new Scanner(System.in);
System.out.println("Enter a positive interger");
number = in.nextInt();
if (number < 0) {
System.out.println("Not a positive interger");
}
else {
System.out.print("Convert to binary is: ");
System.out.print(binaryform(number) + ".");
}
}
private static Object binaryform(int number) {
int remainder;
if (number <= 1) {
System.out.print(number);
return null;
}
remainder = number % 2;
binaryform(number >> 1);
System.out.print(remainder);
{
return " ";
}
}
}
在程序的主要部分中创建了int variable
。在下一部分中说private static Object binaryform ( int number )
。变量I int number
中的Objectrelating
是主要方法吗?
答案 0 :(得分:0)
是和否。变量名number
与main方法无关。它是名为binaryform
的方法的形式参数。参数number
仅存在于方法本身中。但是,当调用binaryform
时,调用中使用的变量(或常量)的实际值在方法执行时变为number
的值。
public class Example {
public static void main(String[] args) {
int n = 3; // the name could be "number" and no behavior would change
Object bf = binaryForm(n);
// do something with bf
}
private static Object binaryform(int number) {
// from the call above, number will have the value 3
Object o = . . .;
// generate or modify o from the value of number
return o;
}
}