目录
- 字符转ASCII码
- 按照格式输入并交换输出
- 出生日期输入输出
- ASCII码
- 字符金字塔
1.题目描述
BoBo教KiKi字符常量或字符变量表示的字符在内存中以ASCII码形式存储。BoBo出了一个问题给KiKi,输入一个字符,输出该字符相应的ASCII码。
1.1输入描述
一行,一个字符。
1.2输出描述
一行,输出输入字符对应的ASCII码。
1.3示例1
输入
c
输出
99
1.4代码实现
import java.util.Scanner;
import java.io.IOException;
public class Main{
public static void main(String[] args) throws IOException{
Scanner input = new Scanner(System.in);
char in = input.next().charAt(0);
System.out.println((int)in);
}
}
2.题目描述
输入两个整数,范围-231~231-1,交换两个数并输出。
2.1输入描述
输入只有一行,按照格式输入两个整数,范围,中间用“,”分隔。
2.2输出描述
把两个整数按格式输出,中间用“,”分隔。
2.3示例1
输入
a=1,b=2
输出
a=2,b=1
2.4代码实现
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String in = input.readLine();
int index = in.indexOf("=");//第一个“=”的位置
String str1 = in.substring(0,index);
String str2 = in.substring(str1.length()+1,in.indexOf(","));//a的值
index = in.indexOf("=",index+1);//第二个“=”的位置
String str3 = in.substring(index+1);//b的值
System.out.println("a="+str3+",b="+str2);
}
}
3.题目描述
输入一个人的出生日期(包括年月日),将该生日中的年、月、日分别输出。
3.1输入描述
输入只有一行,出生日期,包括年月日,年月日之间的数字没有分隔符。
3.2输出描述
三行,第一行为出生年份,第二行为出生月份,第三行为出生日期。输出时如果月份或天数为1位数,需要在1位数前面补0。
3.3示例1
输入
20130225
输出
year=2013
month=02
date=25
3.4代码实现
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String birthday = input.readLine();
String year = birthday.substring(0,4);
System.out.println("year="+year);
String month = birthday.substring(4,6);
System.out.println("month="+month);
String date = birthday.substring(6,8);
System.out.println("date="+date);
}
}
4.题目描述
BoBo教KiKi字符常量或字符变量表示的字符在内存中以ASCII码形式存储。BoBo出了一个问题给KiKi,转换以下ASCII码为对应字符并输出他们。
73, 32, 99, 97, 110, 32, 100, 111, 32, 105, 116 , 33
4.1输入描述
无。
4.2输出描述
转换输出题目中给出的所有ASCII到对应的字符。
4.3代码实现
import java.io.IOException;
public class Main{
public static void main(String[] args) throws IOException{
int input[] = {73,32,99,97,110,32,100,111,32,105,116,33};
for(int i=0;i<input.length;i++){
System.out.print((char)input[i]);
}
}
}
5.题目描述
输入一个字符,用它构造一个三角形金字塔。
5.1输入描述
输入只有一行,一个字符。
5.2输出描述
该字符构成的三角形金字塔。
5.3示例1
输入
1
输出
5.4代码实现
import java.io.IOException;
public class Main{
public static void main(String[] args) throws IOException{
char input = (char)System.in.read();
//将三角形金字塔视为5*5矩阵
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(j>=4-i){
System.out.print(input+" ");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}