【发布时间】:2011-07-13 17:07:02
【问题描述】:
`我不确定要插入什么代码,甚至不知道在哪里,但我想检查我输入的数字是否为奇数。
import java.io.*;
import javax.swing.JOptionPane;
public class Diamond {
public static void main(String [] args) throws IOException {
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
String input;
int num;
System.out.println("input number: ");
input = stdin.readLine ();
num = Integer.parseInt(input);
if (num % 2 ==1){
int d = num;
int e = 0;
for (int a = 0; a <= num; a++) {
for (int c = d; c>= 1; c-- )
System.out.print(" ");
d-=1;
for (int b = 1; b <= a; b++)
System.out.print ("* ");
System.out.println();
}
num-=1;
for (int a = 0; a<=num; a++) {
for (int b = num; b > a; b--)
System.out.print (" *");
System.out.println();
for (int c = 0; c <= e; c++)
System.out.print(" ");
e+=1;
}
} else {
System.out.println("Please enter an odd number!");
}
}
}
【问题讨论】:
-
迈克,你不要把代码放在
[code]和[/code]之间。我刚刚选择了它并点击了代码图标;使用代码块的另一种方法是缩进四个空格。 -
很抱歉,我无法让代码在帖子中正确显示。
-
我加了if-else语句,但觉得不正确。
-
把
input = stdin.readLine (); num = Integer.parseInt(input);拉出if块,num在检查前必须初始化。 -
为什么是社区 wiki 页面?