Java 奇数相加的递归方法
下面是使用递归方法添加奇数和的代码片段。 我已经成功地编写了迭代方法,将用户输入的n和m之间的所有奇数相加。我很想达到这个目标,但起步很慢,以确保我了解正在发生的事情。 我知道以迭代的方式进行比较有意义,不过我正在试验这两种类型,看看哪一种更有效。我被困在下面,因为它没有做我想做的,我不明白为什么Java 奇数相加的递归方法,java,recursion,numbers,sum,Java,Recursion,Numbers,Sum,下面是使用递归方法添加奇数和的代码片段。 我已经成功地编写了迭代方法,将用户输入的n和m之间的所有奇数相加。我很想达到这个目标,但起步很慢,以确保我了解正在发生的事情。 我知道以迭代的方式进行比较有意义,不过我正在试验这两种类型,看看哪一种更有效。我被困在下面,因为它没有做我想做的,我不明白为什么 import java.util.*; public class SumofOdd { public static void main (String [] args) {
import java.util.*;
public class SumofOdd
{
public static void main (String [] args)
{
int n = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter an odd number");
n = sc.nextInt();
int x = add(n);
}
public static int add(int x)
{
if (x == 0)
{
return 0;
}
else
{
return (x + add(x-1));
}
}
}
我已将上述内容更改为以下内容。它编译,但在我输入数字后停止。
导入java.util.*
public class SumofOdd
{
public static void main (String [] args)
{
int n = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter an odd number");
n = sc.nextInt();
if (n%2 == 0)
{
System.out.println("The number entered is even");
}
else
{
int x = add(n);
}
}
public static int add(int x)
{
if (x <= 0)
{
return 0;
}
else
{
return (x + add(x-2));
}
}
}
公共类SumofOdd
{
公共静态void main(字符串[]args)
{
int n=0;
扫描仪sc=新的扫描仪(System.in);
System.out.println(“请输入奇数”);
n=sc.nextInt();
如果(n%2==0)
{
System.out.println(“输入的数字为偶数”);
}
其他的
{
int x=加(n);
}
}
公共静态整数添加(整数x)
{
如果(xpublic int add赔率(int n){
如果(n类奇数和{
public int addodd(int n)
{
if(n<=0)
{
return 0;
}
if(n%2 != 0)
{
return (n+addodd(n-1));
}
else
{
return addodd(n-1);
}
}
}
}
这工作正常:)导入java.util.*;
公共类OddR{
public static void main (String Args [])
{
Scanner s = new Scanner(System.in);
System.out.println("Enter an odd number");
int max = s.nextInt();
if((max% 2) == 0) {
System.out.println(max + " is Even number and therefore is invalid");
}
else{
System.out.println("Enter a greater odd number");
int m = s.nextInt();
if (m <max){
System.out.println("Invalid data");
}
else{
if((m % 2) == 0) {
System.out.println(m + " is Even number and therefore is invalid");
}
else{
int data = (addodd(m)- addodd(max))+max;
System.out.print("sum:"+data);
}
}
}
}
public static int addodd(int m)
{
if(m<=0)
{
return 0;
}
if(m%2 != 0)
{
return (m+addodd(m-1));
}
else
{
return addodd(m-1);
}
}
publicstaticvoidmain(字符串参数[])
{
扫描仪s=新的扫描仪(System.in);
System.out.println(“输入奇数”);
int max=s.nextInt();
如果((最大值%2)=0){
System.out.println(max+“是偶数,因此无效”);
}
否则{
System.out.println(“输入更大的奇数”);
int m=s.nextInt();
if(mpublicstaticvoidmain(String[]args){
公共静态整数oddSum(整数s){
如果(s==0)
返回0;
如果(s<0)
返回0;
其他的
返回s+oddSum(s-2);
}
}
注意:您当前使用的不仅仅是奇数,而是所有的数字,因为add(x)从不检查是奇数还是偶数。使用已经奇数的数字调用add(x-1)将在返回值中添加偶数。首先检查输入数字是否为奇数;第二个问题是x+add(x-1)
不正确,应该是x+add(x-2)//7,5,3,…
最后,“if(x==0)”应该改为if(x)它看起来会停止,因为你没有打印结果吗?虽然这段代码可能会解决问题,但一个好的答案也应该解释代码的作用以及它的帮助。你可以使用答案下面的编辑链接来改进帖子。
public static void main (String[] v)
{
int n = 9;
Oddsum o = new Oddsum();
int data = o.addodd(n);
System.out.print("sum:"+data);
}
public static void main (String Args [])
{
Scanner s = new Scanner(System.in);
System.out.println("Enter an odd number");
int max = s.nextInt();
if((max% 2) == 0) {
System.out.println(max + " is Even number and therefore is invalid");
}
else{
System.out.println("Enter a greater odd number");
int m = s.nextInt();
if (m <max){
System.out.println("Invalid data");
}
else{
if((m % 2) == 0) {
System.out.println(m + " is Even number and therefore is invalid");
}
else{
int data = (addodd(m)- addodd(max))+max;
System.out.print("sum:"+data);
}
}
}
}
public static int addodd(int m)
{
if(m<=0)
{
return 0;
}
if(m%2 != 0)
{
return (m+addodd(m-1));
}
else
{
return addodd(m-1);
}
}
public static void main (String[] args){
public static int oddSum(int s){
if (s == 0)
return 0;
if (s < 0)
return 0;
else
return s + oddSum(s -2);
}
}