[英]If enter n is input and the output is in n digits and starting with (n-1)0 with 1 to (n-1)9's?
I have tried this program many times I didn't get the proper output till now please help me to solve this type of program.我已经尝试了很多次这个程序,直到现在我都没有得到正确的输出,请帮助我解决这种类型的程序。
input:n=3输入:n=3
output: 001 to 999输出:001 到 999
input:n=4输入:n=4
output:0001 to 9999输出:0001 到 9999
input:n=2输入:n=2
output:01 to 99输出:01 到 99
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
int sum=1,result=0;
while(number!=0)
{
result=result+(9*sum);
sum=sum*10;
number--;
}
System.out.println(result);
for(int i=1;i<=result;i++)
{
System.out.printf("%02d ",i);//here i manually mentioned the %02d but i want to take user input
}
}
Can you try below code ?你可以试试下面的代码吗?
class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
String masked = "%0" + (num) + "d%n";
int max = (int)Math.pow(10, num)-1;
for (int k = 1; k <= max; k++)
System.out.printf(masked, k);
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.