switch  case  来判断1到100的整数之间  9出现的次数

解决方案 »

  1.   

    设个for循环,int i,然后将i对10取余,传入switch,然后case  9
      

  2.   

    怎么传入switch
      

  3.   

    public class Demo {
        public static void main(String[] args) {
            //计算9出现的次数
            int count = 0;        //循环遍历1-100
            for (int i = 1; i <= 100; i++) {            //取出个位,十位
                int geWei = i % 10;
                int shiWei = i / 10 % 10;            //判断个位是不是9
                switch (geWei) {
                    case 9:
                        count++;
                        break;
                    default:
                        break;
                }
                //判断十位是不是9
                switch (shiWei) {
                    case 9:
                        count++;
                        break;
                    default:
                        break;
                }
            }
            System.out.println("1-100中9出现" + count + "次");
        }
    }
      

  4.   

         public class Hd {
        public static void main(String[] args) {
            //计算9出现的次数
            int count = 0;
            //循环遍历1-100
            for (int i = 1; i <= 100; i++) {
                //取出个位,十位
                int geWei = i % 10;
                int shiWei = i / 10 % 10;
                //判断个位是不是9
                switch (geWei) {
                    case 9:
                        count++;
                        break;
                }
                //判断十位是不是9
                switch (shiWei) {
                    case 9:
                        count++;
                        break;
                }
            }
            System.out.println("1-100中9出现" + (count-1) + "次");
        }
      

  5.   


    //实现的方式很多种int i = 1;//控制循环条件
    int count = 0;//统计9出现的次数
    while (i <= 100) {//循环100次
    switch (i % 10) {//取余得到出现9的次数
    case 9://出现9,下面的conut加一次
    count++;
    }
    i++;//循环下一次
    }
    System.out.println(count);//打印统计
      

  6.   

    不用switch的话,我觉得我这个可能挺不错的,你可以看看,除了负数,也别超int范围,应该都没啥问题,txt打的,见谅,不过编译运行正常。
    class NineCount {
    public static void main(String[] args){
    int num = 10000;//可自定义一个正整数。
    int count = 0;//计数器。
    int t = 0;//中间变量。
    for(int x=0;x<=num;x++){//使用For循环,从0-num。
    t = x;//把需要判断的数传值中间变量。
    while(t>8){//判断数值是否大于8,即为9或者多位数。
    if((t%10)==9){//看个位是否为9。
    count++;//如果为9则计数器自加1.
    System.out.println(x);//出现一次9,打印一次该数值。
    }
    t/=10;//中间变量整除10,将十位变个位。
    }
    }
    System.out.println("9出现的次数是:"+count);//打印9出现的次数。
    }
    }