题目是要找出输入的数字中的奇数,要求用到method,比如输入12345显示135下面是我的代码,为什么总是提示出错,这个东西到底应该怎么算。。。谁教教我~~谢谢importjava.util.Scanner;...
题目是要找出输入的数字中的奇数,要求用到method,比如输入12345显示135
下面是我的代码,为什么总是提示出错,这个东西到底应该怎么算。。。
谁教教我~~谢谢
import java.util.Scanner;
public class New {
public static void main(String[] args)
{
int choice;
Scanner sc = new Scanner(System.in);
do{
System.out.println("Perform the following methods:");
System.out.println("1:miltiplication test");
System.out.println("2:quotient using division by subtraction");
System.out.println("3.remainder using division by subtraction");
System.out.println("4:count the number of digits");
System.out.println("5:position of a digit");
System.out.println("6:extract all odd digits");
System.out.println("7:quit");
choice=sc.nextInt();
switch(choice){
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
{
long odd,n;
System.out.println("n:");
n=sc.nextLong();
if(n<=0)
System.out.println("Error Input!!");
else
{
odd=extractOddDigits(n);
System.out.println(“oddDigits="+odd);
}
}
break;
case 7:
System.out.println("Program termination...");
}
}while (choice<7);
}
public static long extractOddDigits(long n)
{
long odd=-1,odd1;
double count=1/10;
while(n>0)
{while(n%2==1)
{
odd1=n%10;
count*=10;
n/=10;
odd+=count*odd1;
continue;
}
while(n%2==0)
{
n/=10;
continue;
}
}
while(n==0)
return odd;
}
}
展开