java 我需要将字符串和整数分开,并将其显示在单独的列中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31892579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
i need to separate the string and integer and also to display it in separate columns
提问by augustin l
Java System.out.printf function allowes you to print formatted output. This problem will test your knowledge on this topic.
Java System.out.printf 函数允许您打印格式化输出。这个问题将测试你对这个主题的了解。
Take exactly 3 lines of input. Each line consists of a string and an integer. Suppose this is the sample input:
取 3 行输入。每行由一个字符串和一个整数组成。假设这是样本输入:
java 100 cpp 65 python 50
java 100 cpp 65 python 50
The strings will have at most 10 alphabetic characters and the integers will range between 0 to 999.
字符串最多包含 10 个字母字符,整数范围在 0 到 999 之间。
In each line of output there should be two columns. The string should be in the first column and the integer in the second column. This is the output for the input above:
在每一行输出中应该有两列。字符串应该在第一列中,整数应该在第二列中。这是上面输入的输出:
java 100 cpp 065
爪哇100 cpp 065
python 050
蟒蛇050
The first column should be left justified using exactly 15 characters. The integer of the second column should have exactly 3 digits. If the original input has less than 3 digits, you should pad with zeros to the left.
第一列应使用正好 15 个字符左对齐。第二列的整数应该正好是 3 位数字。如果原始输入少于 3 位数,则应在左侧填充零。
回答by Jigme Thukten
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
for (int i=0;i<2;i++){
String string = scanner.next();
int num = scanner.nextInt();
System.out.printf("%-14s %03d %n", string, num); //note the use of printf
// %-14s fifteen characters left-justified o to 14
// %03d padded with leading zero
}
}
}
}
回答by Chetan Shori
Use this code:
使用此代码:
import java.util.Scanner;
public class Solution
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
for(int i=0; i<3; i++)
{
String s1=sc.next();
int x=sc.nextInt();
System.out.printf("%-14s %03d %n", s1, x);
// %-14s left-justified from o to 14 for string
// %03d padded with leading zero for int
}
}
}
回答by Subhransu Sekhar Das
import java.util.Scanner;
public class Solution {
公共课解决方案{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("================================");
for(int i=0;i<3;i++){
String s1=sc.next();
int x=sc.nextInt();
System.out.printf("%-15s%03d%n",s1,x);
}
System.out.println("================================");
}
}
}
回答by user3664081
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("================================");
for(int i=0;i<3;i++){
String s1=sc.next();
int x=sc.nextInt();
int j=0;
if(s1.length()<=10&&j<1000)
{
String StringFormatted = String.format("%-15s",s1);
System.out.print(StringFormatted);
if(x<100)
{
String padded = String.format("%03d",x);
System.out.print(padded);
}
else
{
System.out.print(x);
}
//System.out.print(x);
}
System.out.println("");
}
System.out.println("================================");
}
回答by Grey Larkan
import java.util.Scanner;
public class Solution {
公共课解决方案{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("================================");
for (int i = 0; i < 3; i++){
String string = sc.next();
int num = sc.nextInt();
System.out.printf("%-14s %03d %n", string, num);
}
System.out.println("================================");
}
}
}
回答by sajad mohammadi
public class asd {
公开课 asd {
public static void main(String[] args) {
String s = "java 100";
int number;
String snum = "";
String text = "";
for (int i = 0; i < s.length(); i++) {
switch (s.charAt(i)) {
case '0':
snum += "0";
break;
case '1':
snum += "1";
break;
case '2':
snum += "2";
break;
case '3':
snum += "3";
break;
case '4':
snum += "4";
break;
case '5':
snum += "5";
break;
case '6':
snum += "6";
break;
case '7':
snum += "7";
break;
case '8':
snum += "8";
break;
case '9':
snum += "9";
break;
default:
text += s.charAt(i);
break;
}
}
number = Integer.parseInt(snum);
System.out.print(text + " " + number);
}
}
}