我是Java的新手,并坚持完成这项任务,虽然我已经以某种方式完成了它,但我想在没有if-else
语句的情况下完成它。
for(int i=0; i<numRows; i++)
{
for(int j=0; j<numRows-i; j++)
{
System.out.print(" ");
}
for(int k = 0; k <= i*2; k++)
{
if(k%2 == 1)
System.out.print("A");
else
System.out.print("*");
}
System.out.println();
}
输出:
*
*A*
*A*A*
*A*A*A*
*A*A*A*A*
答案 0 :(得分:2)
public static void main(String[] args) {
int numRows = 5;
String s = "*A*";
String space = "";
for (int i = 0; i < numRows - 1; i++) {
space += " ";
}
System.out.println(space + "*" + space);
for (int i = 0; i < numRows - 1; i++) {
String str = space.substring(0, space.length() - i - 1);
System.out.println(str + s + str);
s += "A*";
}
}
答案 1 :(得分:2)
由于每行以星号开头,然后重复A*
n次(其中n是行号-1,即第一行为0,第二行为1等),您可以在循环中执行此操作,即打印第一个星号,然后根据需要重复A*
。
示例:
for( int i = 0; i < numRows; i++ )
{
//print whitespace for correct indentation
for( int j = 0; j < numRows - i; j++ )
{
System.out.print( " " );
}
//print the first asterisk
System.out.print( "*" );
//print as many A* as needed, i.e. row-1 times
for( int k = 0; k < i; k++ )
{
System.out.print( "A*" );
}
System.out.println();
}
答案 2 :(得分:0)
您将if-else更改为以下代码中提到的行
int numRows=5;
for(int i=0; i<numRows; i++)
{
for(int j=0; j<numRows-i; j++)
{
System.out.print(" ");
}
for(int k = 0; k <= i*2; k++)
{
//calculates according to mod value of k
char ch=(char) (((k%2)*'A')+((1-k%2)*'*'));
System.out.print(ch);
}
System.out.println();
}
答案 3 :(得分:-1)
代码第1部分
String nom,silaba;
int nomlong,longi,z=0;
char ass;
nom=txt1.getText();
nomlong=nom.length();
txtapex.setText("");
do{
silaba=nom.substring(0+z, 1+z);
char c=silaba.charAt(0);
int ascii = (int) c;
switch(ascii){
case 65:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(i+j==0){
txtapex.append(" ");
}else if(j-i==-3){
txtapex.append(" ");
}else if(i==0){
txtapex.append("8 ");
}else if(i==3){
txtapex.append("8 ");
}else if(j==0){
txtapex.append("8 ");
}else if(j==2){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 97:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(i+j==0){
txtapex.append(" ");
}else if(j-i==-3){
txtapex.append(" ");
}else if(i==0){
txtapex.append("8 ");
}else if(i==3){
txtapex.append("8 ");
}else if(j==0){
txtapex.append("8 ");
}else if(j==2){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 66:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(i==0){
txtapex.append("8 ");
}else if(j-i==-3){
txtapex.append(" ");
}else if(j==0){
txtapex.append("8 ");
}else if(j-i==-2){
txtapex.append("8 ");
}else if(j==1){
txtapex.append(" ");
}else if(j-i==-1){
txtapex.append(" ");
}else if(j==2){
txtapex.append("8 ");
}else if(j+i==6){
txtapex.append("8 ");
}else if(j+i==7){
txtapex.append(" ");
}else if(j==4){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 98:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(i==0){
txtapex.append("8 ");
}else if(j-i==-3){
txtapex.append(" ");
}else if(j==0){
txtapex.append("8 ");
}else if(j-i==-2){
txtapex.append("8 ");
}else if(j==1){
txtapex.append(" ");
}else if(j-i==-1){
txtapex.append(" ");
}else if(j==2){
txtapex.append("8 ");
}else if(j+i==6){
txtapex.append("8 ");
}else if(j+i==7){
txtapex.append(" ");
}else if(j==4){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 67:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(i+j==0){
txtapex.append(" ");
}else if(j-i==4){
txtapex.append(" ");
}else if(i==0){
txtapex.append("8 ");
}else if(j-i==3){
txtapex.append("8 ");
}else if(j==0){
txtapex.append("8 ");
}else if(j==4){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 99:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(i+j==0){
txtapex.append(" ");
}else if(j-i==4){
txtapex.append(" ");
}else if(i==0){
txtapex.append("8 ");
}else if(j-i==3){
txtapex.append("8 ");
}else if(j==0){
txtapex.append("8 ");
}else if(j==4){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 68:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(j-i==-3){
txtapex.append(" ");
}else if(i+j==7){
txtapex.append(" ");
}else if(i==0){
txtapex.append("8 ");
}else if(i==3){
txtapex.append("8 ");
}else if(j==0){
txtapex.append("8 ");
}else if(j==4){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 100:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(j-i==-3){
txtapex.append(" ");
}else if(i+j==7){
txtapex.append(" ");
}else if(i==0){
txtapex.append("8 ");
}else if(i==3){
txtapex.append("8 ");
}else if(j==0){
txtapex.append("8 ");
}else if(j==4){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 69:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(j*i==0){
txtapex.append("8 ");
}else if(j==2){
txtapex.append("8 ");
}else if(j==4){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 101:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(j*i==0){
txtapex.append("8 ");
}else if(j==2){
txtapex.append("8 ");
}else if(j==4){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 70:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(j*i==0){
txtapex.append("8 ");
}else if(j==2){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 102:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(j*i==0){
txtapex.append("8 ");
}else if(j==2){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 71:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(j+i==0){
txtapex.append(" ");
}else if(j-i==4){
txtapex.append(" ");
}else if(j==0){
txtapex.append("8 ");
}else if(i==0){
txtapex.append("8 ");
}else if(j==4){
txtapex.append("8 ");
}else if(j+i==3){
txtapex.append(" ");
}else if(j==2){
txtapex.append("8 ");
}else if(j==4){
txtapex.append("8 ");
}else if(j*i==3){
txtapex.append(" ");
}else if(i==3){
txtapex.append("8 ");
}else if(j*i==3){
txtapex.append(" ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 103:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(j+i==0){
txtapex.append(" ");
}else if(j-i==4){
txtapex.append(" ");
}else if(j==0){
txtapex.append("8 ");
}else if(i==0){
txtapex.append("8 ");
}else if(j==4){
txtapex.append("8 ");
}else if(j+i==3){
txtapex.append(" ");
}else if(j==2){
txtapex.append("8 ");
}else if(j==4){
txtapex.append("8 ");
}else if(j*i==3){
txtapex.append(" ");
}else if(i==3){
txtapex.append("8 ");
}else if(j*i==3){
txtapex.append(" ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 72:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(i==0){
txtapex.append("8 ");
}else if(i==3){
txtapex.append("8 ");
}else if(j==2){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 104:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(i==0){
txtapex.append("8 ");
}else if(i==3){
txtapex.append("8 ");
}else if(j==2){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 73:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(j-i==4){
txtapex.append(" ");
}else if(j+i==0){
txtapex.append(" ");
}else if(j==0){
txtapex.append("8 ");
}else if(j==4){
txtapex.append("8 ");
}else if(i==2){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 105:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(j-i==4){
txtapex.append(" ");
}else if(j+i==0){
txtapex.append(" ");
}else if(j==0){
txtapex.append("8 ");
}else if(j==4){
txtapex.append("8 ");
}else if(i==2){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 74:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(j==0){
txtapex.append("8 ");
}else if(j-i==2){
txtapex.append(" ");
}else if(j-i==3){
txtapex.append("8 ");
}else if(i==2){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 106:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(j==0){
txtapex.append("8 ");
}else if(j-i==2){
txtapex.append(" ");
}else if(j-i==3){
txtapex.append("8 ");
}else if(i==2){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 75:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(i==0){
txtapex.append("8 ");
}else if(j-i==1){
txtapex.append("8 ");
}else if(j+i==3){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 107:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(i==0){
txtapex.append("8 ");
}else if(j-i==1){
txtapex.append("8 ");
}else if(j+i==3){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 76:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(i==0){
txtapex.append(" ");
}else if(i==1){
txtapex.append("8 ");
}else if(j==4){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;
case 108:
for(int j=0;j<5;j++){
for (int i = 0; i < 4; i++) {
if(i==0){
txtapex.append(" ");
}else if(i==1){
txtapex.append("8 ");
}else if(j==4){
txtapex.append("8 ");
}else{
txtapex.append(" ");
}
}
txtapex.append("\n");
}
txtapex.append("\n");
break;