Java - 垂直整数和回文
Java - vertical integers and palindrome
我偶然发现了一个练习,要求我重现这个(这是预期的输出):
11111
3456789012109876543
这是一个回文(在底部),其中大于 9(两位数)的数字必须垂直书写。这对我来说听起来很复杂,我需要一些帮助。
这是我目前所做的,回文:
class Print {
public static void main(String[] args) {
System.out.println("Insert a number from 1 to 100: ");
int input = Read.anInt();
System.out.println("Insert another number from 1 to 100: ");
int output = Read.anInt();
int a = input;
for (int i = a; i < output; i++){
System.out.print(a);
a++;
}
a = input -1;
for (int j = output; j > a; j--){
System.out.print(output);
output--;
}
}
}
你能帮我解释一下如何确保竖写大于 9 的数字吗?
AdamRice:我的意思是:
3456789111119876543
01210
但到目前为止我设法做到的是这个烂摊子:
456789101
0
111
1
121110987654
这可能是因为我完全忽略了数组。
抱歉有点慢。终于明白问题所在后,我想我有办法了。
import java.util.Scanner;
public class VerticalText {
public static void main(String[] args) {
Scanner Read = new Scanner(System.in);
System.out.println("Insert a number from 1 to 100: ");
int start = Read.nextInt();
System.out.println("Insert another number from 1 to 100: ");
int end = Read.nextInt();
String numbers = "";
for(int i = start; i <= end; i++)
{
if(i < 10)
{
numbers += String.format("%02d", i);
}
else
{
numbers += i;
}
}
for(int i = (end-1); i >= start; i--)
{
if(i < 10)
{
numbers += String.format("%02d", i);
}
else
{
numbers += i;
}
}
String row1 = "";
String row2 = "";
char[] chars = numbers.toCharArray();
for(int i = 0; i < chars.length; i++)
{
if(chars[i] == '0')
{
chars[i] = ' ';
}
row1 += chars[i];
i++;
row2 += chars[i];
}
System.out.println(row1);
System.out.println(row2);
}
}
使用输入 5 和 15,它产生了以下输出:
11111111111
567890123454321098765
说明
我构建了一个数字字符串,如果它小于 10,则将其格式化为前导 0。这个额外的 0 只是一个占位符。打印时,我们可以打印 space 而不是零。
我偶然发现了一个练习,要求我重现这个(这是预期的输出):
11111
3456789012109876543
这是一个回文(在底部),其中大于 9(两位数)的数字必须垂直书写。这对我来说听起来很复杂,我需要一些帮助。
这是我目前所做的,回文:
class Print {
public static void main(String[] args) {
System.out.println("Insert a number from 1 to 100: ");
int input = Read.anInt();
System.out.println("Insert another number from 1 to 100: ");
int output = Read.anInt();
int a = input;
for (int i = a; i < output; i++){
System.out.print(a);
a++;
}
a = input -1;
for (int j = output; j > a; j--){
System.out.print(output);
output--;
}
}
}
你能帮我解释一下如何确保竖写大于 9 的数字吗?
AdamRice:我的意思是:
3456789111119876543
01210
但到目前为止我设法做到的是这个烂摊子:
456789101
0
111
1
121110987654
这可能是因为我完全忽略了数组。
抱歉有点慢。终于明白问题所在后,我想我有办法了。
import java.util.Scanner;
public class VerticalText {
public static void main(String[] args) {
Scanner Read = new Scanner(System.in);
System.out.println("Insert a number from 1 to 100: ");
int start = Read.nextInt();
System.out.println("Insert another number from 1 to 100: ");
int end = Read.nextInt();
String numbers = "";
for(int i = start; i <= end; i++)
{
if(i < 10)
{
numbers += String.format("%02d", i);
}
else
{
numbers += i;
}
}
for(int i = (end-1); i >= start; i--)
{
if(i < 10)
{
numbers += String.format("%02d", i);
}
else
{
numbers += i;
}
}
String row1 = "";
String row2 = "";
char[] chars = numbers.toCharArray();
for(int i = 0; i < chars.length; i++)
{
if(chars[i] == '0')
{
chars[i] = ' ';
}
row1 += chars[i];
i++;
row2 += chars[i];
}
System.out.println(row1);
System.out.println(row2);
}
}
使用输入 5 和 15,它产生了以下输出:
11111111111
567890123454321098765
说明 我构建了一个数字字符串,如果它小于 10,则将其格式化为前导 0。这个额外的 0 只是一个占位符。打印时,我们可以打印 space 而不是零。