注册 登录
编程论坛 JAVA论坛

求助 代码能运行 但是运行时有问题 输入数据不能输出结果 要怎么修改呢

遗情处有诗章 发布于 2018-03-27 18:52, 3249 次点击
实现一个函数,可统计任一整数中某个数字出现的次数。例如21252中,2出现了3次,则该函数应该返回3。int countDigit (int n, int d);
程序代码:
import java.util.Scanner;
public class Test {
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int d=in.nextInt();
        System.out.print(countDigit(n, d));
}
public static int countDigit( int n, int d)
    {
        Scanner in=new Scanner(System.in);
        int count=in.nextInt();
        int temp=in.nextInt();
        int x=in.nextInt();
     x=n;
     if(x<0)
     x=-x;
     do{
     temp=x%10;
     if(temp==d)
     count++;
         x=x/10;
     }
     while(x>0);      
     return count;
    }     
}



给定两个均不超过9的正整数a和n,要求编写函数求a+aa+aaa++⋯+aa⋯a(n个a)之和。函数接口定义:int fn( int a, int n ); int SumA( int a, int n );

其中函数fn须返回的是n个a组成的数字,比如,fn(2,3)返回的是222;
SumA返回要求的和,比如sumA(2,3)返回2 + 22 + 222。


程序代码:
import java.util.Scanner;
public class Test {
    static int fn( int a, int n ) {
        
        Scanner in=new Scanner(System.in);
        
        int sum = in.nextInt();
        sum=0;
        int b = in.nextInt();
        b=1;   
    for(int i=0;i<n;i++)
    {
    //b=b*10;
    sum=sum+a*b;
    b=b*10;
    }
    return sum;
    }
    static int SumA( int a, int n ) {
        Scanner in=new Scanner(System.in);
        int sum1 = in.nextInt();
        sum1=0;
        int sum2 = in.nextInt();
    for(int i=1;i<n+1;i++)
    {
    sum2=fn(a,i);
    sum1=sum1+sum2;

    }
    return sum1;
public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        int a = in.nextInt();
        int n = in.nextInt();
         
         
         System.out.println(fn(a,n));
         System.out.println(SumA(a,n));
         
    }
}


8 回复
#2
遗情处有诗章2018-03-27 19:12
两个题目都是能出现运行栏 输东西进去没反应
#3
疯狂的小a2018-03-27 19:58
程序代码:
import java.util.Scanner;

public class Test {
    /*实现一个函数,可统计任一整数中某个数字出现的次数。例如21252中,2出现了3次,则该函数应该返回3。int countDigit (int n, int d); */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入一个整数:");
        String str = sc.nextLine();
        System.out.print("请输入所求数字:");
        String str2 = sc.nextLine();
        
        String n = str2.trim().charAt(0)+"";
        
        int count = 0;
        for(;;) {
            if(str.contains(n)) {
                str = str.replaceFirst(n,",,,");
                count++;
            }else {
                break;
            }
        }
        System.out.println("数字:"+str2+"出现了:"+count+"次");
        sc.close();
    }
}
#4
遗情处有诗章2018-03-27 20:13
回复 3楼 疯狂的小a
int countDigit( int n, int d)题目里有要求要用到这个🙌
#5
疯狂的小a2018-03-27 20:14
程序代码:
import java.util.Scanner;
/*给定两个均不超过9的正整数a和n,要求编写函数求a+aa+aaa++⋯+aa⋯a(n个a)之和。函数接口定义:int fn( int a, int n ); int SumA( int a, int n );

其中函数fn须返回的是n个a组成的数字,比如,fn(2,3)返回的是222;
SumA返回要求的和,比如sumA(2,3)返回2 + 22 + 222。
*/

public class Test {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入a:");
        int a = sc.nextInt();
        System.out.print("请输入n:");
        int n = sc.nextInt();

        String str1 = fn(a, n);
        String str2 = sumA(a, n);

        System.out.println(str1);
        System.out.println(str2);
    }

    public static String sumA(int a, int n) {
        String str = "";
        for (int i = n; i > 0; i--) {
            for (int j = 0; j < n; j++) {
                str = ("" + a) + str;
            }
            n--;
            str = "+" + str;
        }
        return str;
    }

    public static String fn(int a, int n) {
        String str = "";
        for (int i = 0; i < n; i++) {
            str += ("" + a);
        }
        return str;
    }
}
#6
桃花岛主丶2018-03-27 22:44
回复 3楼 疯狂的小a
每次看你的头像都感觉好搞笑
#7
遗情处有诗章2018-03-28 08:32
回复 5楼 疯狂的小a
我解决好啦 谢谢

[此贴子已经被作者于2018-3-28 10:09编辑过]

#8
有多难2018-03-28 12:04
回复 6楼 桃花岛主丶
感觉非一般的特步
#9
a1530qp2018-03-28 12:08
今天刚来 还想赚点
1