java 给出一个整数N,输出10个数字:(1--N之间含有多少0,1,2,3,4,5,6,7,8,9)RT,想了挺久不知道怎么弄,感觉要靠数字长度来算,
来源:学生作业帮助网 编辑:六六作业网 时间:2025/01/30 21:28:17
java 给出一个整数N,输出10个数字:(1--N之间含有多少0,1,2,3,4,5,6,7,8,9)RT,想了挺久不知道怎么弄,感觉要靠数字长度来算,
java 给出一个整数N,输出10个数字:(1--N之间含有多少0,1,2,3,4,5,6,7,8,9)
RT,想了挺久不知道怎么弄,感觉要靠数字长度来算,
java 给出一个整数N,输出10个数字:(1--N之间含有多少0,1,2,3,4,5,6,7,8,9)RT,想了挺久不知道怎么弄,感觉要靠数字长度来算,
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author yugi111 <br>
* compute how many 0-9
*/
public class ComputeHowManyNums
{
public static void main( String[] args )
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputTip = "please input N numbers:";
System.out.println(inputTip);
String N = "0";
try
{
while(!( N = (br.readLine()).trim() ).matches("^[1-9]\\d+|\\d$"))
{
System.out.println(inputTip);
}
}
catch(IOException e)
{
e.printStackTrace();
}
// from 1 to N
String result = " ";
for( int i = 1; i <= Integer.parseInt(N); i++ )
{
result += i;
}
result += " ";
System.out.println("result is :" + result);
// compute 0-9
for( int i = 0; i < 10; i++ )
{
System.out.println(i + " : " + (result.split(String.valueOf(i)).length - 1));
}
}
}