JAVA统计整数number中数字digit的个数
程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat(0<repeat<10),做repeat次下列运算:读入1个整数,统计并输出该数中2的个数。要求定义并...
程序填空,不要改变与输入输出有关的语句。
输入一个正整数repeat (0<repeat<10),做repeat次下列运算:
读入1 个整数,统计并输出该数中2的个数。
要求定义并调用函数countdigit(number,digit),它的功能是统计整数number中数字digit的个数。例如,countdigit(10090,0)的返回值是3。
例:括号内是说明
输入:
3 (repeat=3)
-21902
2
345543
输出:
count=2 (-21902中有2个2)
count=1 (有1个2)
count=0 (345543中没有2)
import java.util.Scanner;
public class Test50003{
public static void main(String args[]){
int ri, repeat;
int count;
long n;
Scanner in=new Scanner(System.in);
repeat=in.nextInt();
for(ri=1; ri<=repeat; ri++){
n=in.nextInt();
/*---------*/
System.out.println("count="+count);
}
}
/*------------*/
}
一楼的TX错了=。=虽然我很想给你分· 展开
输入一个正整数repeat (0<repeat<10),做repeat次下列运算:
读入1 个整数,统计并输出该数中2的个数。
要求定义并调用函数countdigit(number,digit),它的功能是统计整数number中数字digit的个数。例如,countdigit(10090,0)的返回值是3。
例:括号内是说明
输入:
3 (repeat=3)
-21902
2
345543
输出:
count=2 (-21902中有2个2)
count=1 (有1个2)
count=0 (345543中没有2)
import java.util.Scanner;
public class Test50003{
public static void main(String args[]){
int ri, repeat;
int count;
long n;
Scanner in=new Scanner(System.in);
repeat=in.nextInt();
for(ri=1; ri<=repeat; ri++){
n=in.nextInt();
/*---------*/
System.out.println("count="+count);
}
}
/*------------*/
}
一楼的TX错了=。=虽然我很想给你分· 展开
3个回答
展开全部
照你的要求写出来了。不过这个程序真的有点怪,很想把 n = in.nextInt(); 改成 n = in.nextLong(); 其实更想改成 next() 然后 countdigit 直接传字符串,转换都省了。
-------------------------------------------------
import java.util.Scanner;
public class Test50003{
public static void main(String args[]){
int ri, repeat;
int count;
long n;
Scanner in=new Scanner(System.in);
repeat=in.nextInt();
for(ri=1; ri<=repeat; ri++){
n=in.nextInt();
count = countdigit(n, 2);
System.out.println("count="+count);
}
}
public static int countdigit(long number, int digit){
int count = 0;
String str = number + "";
byte[] chars = str.getBytes();
for (byte b : chars) {
if (digit == b - '0')
count ++;
}
return count;
}
}
-------------------------------------------------
import java.util.Scanner;
public class Test50003{
public static void main(String args[]){
int ri, repeat;
int count;
long n;
Scanner in=new Scanner(System.in);
repeat=in.nextInt();
for(ri=1; ri<=repeat; ri++){
n=in.nextInt();
count = countdigit(n, 2);
System.out.println("count="+count);
}
}
public static int countdigit(long number, int digit){
int count = 0;
String str = number + "";
byte[] chars = str.getBytes();
for (byte b : chars) {
if (digit == b - '0')
count ++;
}
return count;
}
}
展开全部
哪里错了??
import java.util.Scanner;
public class Test50003 {
public static void main(String args[]) {
int ri, repeat;
int count;
long n;
Scanner in = new Scanner(System.in);
repeat = in.nextInt();
for (ri = 1; ri <= repeat; ri++) {
n = in.nextInt();
/*---------*/
count = countdigit(n,2);
/*---------*/
System.out.println("count=" + count);
}
}
/*------------*/
public static int countdigit(long number,int digit){
int count = 0;
long n = Math.abs(number);
do{
if(n%10==digit){
count++;
}
n /= 10;
}while(n>0);
return count;
}
/*------------*/
}
import java.util.Scanner;
public class Test50003 {
public static void main(String args[]) {
int ri, repeat;
int count;
long n;
Scanner in = new Scanner(System.in);
repeat = in.nextInt();
for (ri = 1; ri <= repeat; ri++) {
n = in.nextInt();
/*---------*/
count = countdigit(n,2);
/*---------*/
System.out.println("count=" + count);
}
}
/*------------*/
public static int countdigit(long number,int digit){
int count = 0;
long n = Math.abs(number);
do{
if(n%10==digit){
count++;
}
n /= 10;
}while(n>0);
return count;
}
/*------------*/
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Test50003 {
public static void main(String args[]) {
int ri, repeat;
int count = 0;
long n;
Scanner in = new Scanner(System.in);
repeat = in.nextInt();
for (ri = 1; ri <= repeat; ri++) {
n = in.nextInt();
/*---------*/
count = countdigit(n, 2);
System.out.println("count=" + count);
}
}
private static int countdigit(long n, int digit) {
char c = (char) (digit + 48);
int count = 0;
String str = n + "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == c) {
count++;
}
}
return count;
}
}
public static void main(String args[]) {
int ri, repeat;
int count = 0;
long n;
Scanner in = new Scanner(System.in);
repeat = in.nextInt();
for (ri = 1; ri <= repeat; ri++) {
n = in.nextInt();
/*---------*/
count = countdigit(n, 2);
System.out.println("count=" + count);
}
}
private static int countdigit(long n, int digit) {
char c = (char) (digit + 48);
int count = 0;
String str = n + "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == c) {
count++;
}
}
return count;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询