使用用户输入绘制钻石(不是用户输入的两倍)
Drawing a Diamond with User Input (not double of the user input)
所以我一直在尝试在 Java 中绘制钻石形状,我已经完成了钻石的顶部,但底部没有按照我想要的方式打印。它不会在最后减少,而是保持不变或随着下降而增加。
import java.util.Scanner;
public class Q1_Diamond{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("How many lines? ");
int lines = sc.nextInt();
// Top half of Diamond
for(int i = 0; i < lines/2; i++){
for(int j = 0; j < (lines-1)-i; j++){
System.out.print(" ");
}
for(int j = 0; j < i; j++){
System.out.print("*");
}
for(int j = 0; j < i+1; j++){
System.out.print("*");
}
System.out.println();
}
// Bottom half of Diamond
// Even number of lines
if(lines % 2 == 0){
for(int k = 0; k < (lines/2); k++){
for(int j = 0; j < (lines/2)+k; j++){
System.out.print(" ");
}
for(int j = 0; j < (lines/3 - 0.5f); j++){
System.out.print("*");
}
for(int j = 0; j < lines/2+1; j++){
System.out.print("*");
}
System.out.println();
}
}
// Odd number of lines
else{
not done yet...
}
}
}
删除下半部分的 if
条件(即 if(lines % 2 == 0)
),并使用以下循环声明简单地重复与上半部分相同的代码:
for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--)
完整代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many lines? ");
int lines = sc.nextInt();
// Top half of Diamond
for (int i = 0; i < lines / 2; i++) {
for (int j = 0; j < (lines - 1) - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
// Bottom half of Diamond
for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--) {
for (int j = 0; j < (lines - 1) - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
样本运行:
How many lines? 10
*
***
*****
*******
*********
*********
*******
*****
***
*
另一个样本运行:
How many lines? 11
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
[更新]
您可以将通用代码提取到一个方法中,例如printLine
如下图:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many lines? ");
int lines = sc.nextInt();
// Top half of Diamond
for (int i = 0; i < lines / 2; i++) {
printLine(lines, i);
}
// Bottom half of Diamond
for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--) {
printLine(lines, i);
}
}
static void printLine(int lines, int i) {
for (int j = 0; j < (lines - 1) - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
解决这个问题的另一种方法是确定星号每行中空格和星号数量的表达式。显然,这需要根据行数 (n) 和我们所在的行来计算。
如果我们对从 i = 0 到 n-1 的行进行编号,我们得到
nAsterisks = 1 + 2 * min(i, n-i-1)
和
nSpaces = (n+1)/2 - 1 - min(i, n-i-1)
有了这些我们可以写一些紧凑的Java:
static void printStar(int n)
{
for(int i=0, k=0; i<n; i++, k=Math.min(i, n-i-1))
{
for(int j=0; j<(n+1)/2-1-k; j++) System.out.print(" ");
for(int j=0; j<1+2*k; j++) System.out.print("*");
System.out.println();
}
}
对于printStar(8)
,我们得到:
*
***
*****
*******
*******
*****
***
*
和printStar(9)
给出
*
***
*****
*******
*********
*******
*****
***
*
所以我一直在尝试在 Java 中绘制钻石形状,我已经完成了钻石的顶部,但底部没有按照我想要的方式打印。它不会在最后减少,而是保持不变或随着下降而增加。
import java.util.Scanner;
public class Q1_Diamond{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("How many lines? ");
int lines = sc.nextInt();
// Top half of Diamond
for(int i = 0; i < lines/2; i++){
for(int j = 0; j < (lines-1)-i; j++){
System.out.print(" ");
}
for(int j = 0; j < i; j++){
System.out.print("*");
}
for(int j = 0; j < i+1; j++){
System.out.print("*");
}
System.out.println();
}
// Bottom half of Diamond
// Even number of lines
if(lines % 2 == 0){
for(int k = 0; k < (lines/2); k++){
for(int j = 0; j < (lines/2)+k; j++){
System.out.print(" ");
}
for(int j = 0; j < (lines/3 - 0.5f); j++){
System.out.print("*");
}
for(int j = 0; j < lines/2+1; j++){
System.out.print("*");
}
System.out.println();
}
}
// Odd number of lines
else{
not done yet...
}
}
}
删除下半部分的 if
条件(即 if(lines % 2 == 0)
),并使用以下循环声明简单地重复与上半部分相同的代码:
for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--)
完整代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many lines? ");
int lines = sc.nextInt();
// Top half of Diamond
for (int i = 0; i < lines / 2; i++) {
for (int j = 0; j < (lines - 1) - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
// Bottom half of Diamond
for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--) {
for (int j = 0; j < (lines - 1) - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
样本运行:
How many lines? 10
*
***
*****
*******
*********
*********
*******
*****
***
*
另一个样本运行:
How many lines? 11
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
[更新]
您可以将通用代码提取到一个方法中,例如printLine
如下图:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many lines? ");
int lines = sc.nextInt();
// Top half of Diamond
for (int i = 0; i < lines / 2; i++) {
printLine(lines, i);
}
// Bottom half of Diamond
for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--) {
printLine(lines, i);
}
}
static void printLine(int lines, int i) {
for (int j = 0; j < (lines - 1) - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
解决这个问题的另一种方法是确定星号每行中空格和星号数量的表达式。显然,这需要根据行数 (n) 和我们所在的行来计算。
如果我们对从 i = 0 到 n-1 的行进行编号,我们得到
nAsterisks = 1 + 2 * min(i, n-i-1)
和
nSpaces = (n+1)/2 - 1 - min(i, n-i-1)
有了这些我们可以写一些紧凑的Java:
static void printStar(int n)
{
for(int i=0, k=0; i<n; i++, k=Math.min(i, n-i-1))
{
for(int j=0; j<(n+1)/2-1-k; j++) System.out.print(" ");
for(int j=0; j<1+2*k; j++) System.out.print("*");
System.out.println();
}
}
对于printStar(8)
,我们得到:
*
***
*****
*******
*******
*****
***
*
和printStar(9)
给出
*
***
*****
*******
*********
*******
*****
***
*