【发布时间】:2014-08-26 19:16:13
【问题描述】:
我在理解本练习中应该如何执行 compute_even 方法时遇到了一些问题,希望有人能帮助我。
别在意 compute_odd 方法,我还在想那个!
这是练习:
编写一个名为 choose_function 的 void 方法,它有一个 int 类型的参数 n。如果 n 的值是偶数,则该方法将调用方法 compute_even 并将参数 n 的值传递给它,否则该方法将调用方法 compute_odd 并将 n 的值传递给它。
这两种方法将在控制台上按以下顺序打印:
compute_even:2、4、8、16、32、64、128……最多 n
compute_odd: 1, 3, 6, 10, 15, 21, 28 … 最多 n
编写程序,让用户输入一个大于零的整数 n1(因此程序会提示用户输入一个值,直到条件不满足)。 程序将在控制台上打印与 n1 的值相关的序列。
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n1;
do
{
System.out.println("Enter a positive integer value: ");
n1 = input.nextInt();
}while(n1 <= 0);
choose_function(n1);
input.close();
}
public static void choose_function(int n)
{
if(n%2 == 0)
System.out.print(compute_even(n));
else
System.out.print(compute_odd(n));
}
public static int compute_even(int k)
{
int r = 1;
do
{
r = r*2;
return r;
}while(r <= k);
}
public static int compute_odd(int k)
{
}
强文本
【问题讨论】:
-
您需要打印从零到 n1 的所有偶数/奇数吗?
标签: java methods computation