题目描述;
代码如下:
import javax.swing.plaf.synth.SynthScrollBarUI;
import java.util.Scanner;/*** Created with IntelliJ IDEA* Description:跟奥巴马一起编程,输出正方形* User:Light_Long* Date:2019-07-15* Time:11:05*/
public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);String str = in.nextLine();String[] str2 = new String[str.length()];int pop = 0;int low = 0;for (int i = 0; i < str.length(); i++) {if(str.charAt(i) != ' '){}else{str2[pop] = "";for (int j = low; j < i; j++) {str2[pop] += str.charAt(j); //获取截取的第一个字符串}pop++;low = i + 1;}}str2[pop] = "";for (int j = low; j < str.length(); j++) {str2[pop] += str.charAt(j); //获取截取的第二个字符串}int nums = Integer.parseInt(str2[0]); //边长转换为能用的整数String key = str2[1];for (int i = 0; i < (nums + 1) / 2; i++) { //列数为四舍五入之后的数字,所以nums+1if(i == 0 || i == (nums + 1) / 2 - 1){ //第一行和最后一行需要输出一整行for (int j = 0; j < nums; j++) {System.out.print(key);}if(i == 0){System.out.println(); //第一行之后需要换行而最后一行不用}}else{for (int j = 0; j < nums; j++) {if(j == 0 || j == nums - 1){ //只有第一个和最后一个字符需要输出,其他的则是空格即可System.out.print(key);}else{System.out.print(' ');}}System.out.println(); //每行输出完成之后需要换行}}}
}
本文发布于:2024-01-30 23:49:48,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170662979123723.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |