我刚学java不久,下面的代码,看不懂,有没有高手帮我解释一下.
第一段代码:publicclasshello{publicstaticvoidmain(Stringargs[]){/*intlayer=4;for(inti=1;i<=...
第一段代码:
public class hello
{
public static void main(String args[])
{
/*int layer =4;
for (int i=1;i<=layer ;i++ )
{
for (int k=1;k<=layer-i ;k++ )
{
System.out.print(" ");
}
for (int j=1;j<=(i-1)*2+1 ;j++ )
{
System.out.print("*");
}
System.out.println();
}
int layer1 =3;
for (int i=1;i<=layer1 ;i++ )
{
for (int k=1;k<=(layer1-i)+1 ;k++ )
{
System.out.print(" ");
}
for (int j=5;j>=(i+1)*2+1 ;j++)
{
System.out.print("*");
}
System.out.println();
第二段代码:
public class hello
{
pubic static void main(String args[])
{
int i1,j1;
for(i1=1;i1<=7;i1++){
for(j1=1;j1<=7;j1++){
if(j1+i1==5||j1-i1==3||i1-j1==3||i1+j1==11){
System.out.print("*");
}else{System.out.print("");}}
System.out.println();
}}
第三段代码:
习题:用类的形为方法编写,键盘接收任一一个数,要求打印到该数的乘法表
import java.util.Scanner;
public class hello {
private int maxNum;
public hello(int maxNum) {
this.maxNum = maxNum;
}
public void print() {
for (int i = 1; i <= maxNum; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
System.out.print("*");
System.out.print(i);
System.out.print("=");
System.out.print(i * j);
System.out.print("\t");
}
System.out.println();
}
}
public static void main(String[] args) {
System.out.print("录入一个整数:");
Scanner input = new Scanner(System.in);
// 没有考虑判断输入的是否是整数。
int maxNum = input.nextInt();
new hello(maxNum).print();
}
} 展开
public class hello
{
public static void main(String args[])
{
/*int layer =4;
for (int i=1;i<=layer ;i++ )
{
for (int k=1;k<=layer-i ;k++ )
{
System.out.print(" ");
}
for (int j=1;j<=(i-1)*2+1 ;j++ )
{
System.out.print("*");
}
System.out.println();
}
int layer1 =3;
for (int i=1;i<=layer1 ;i++ )
{
for (int k=1;k<=(layer1-i)+1 ;k++ )
{
System.out.print(" ");
}
for (int j=5;j>=(i+1)*2+1 ;j++)
{
System.out.print("*");
}
System.out.println();
第二段代码:
public class hello
{
pubic static void main(String args[])
{
int i1,j1;
for(i1=1;i1<=7;i1++){
for(j1=1;j1<=7;j1++){
if(j1+i1==5||j1-i1==3||i1-j1==3||i1+j1==11){
System.out.print("*");
}else{System.out.print("");}}
System.out.println();
}}
第三段代码:
习题:用类的形为方法编写,键盘接收任一一个数,要求打印到该数的乘法表
import java.util.Scanner;
public class hello {
private int maxNum;
public hello(int maxNum) {
this.maxNum = maxNum;
}
public void print() {
for (int i = 1; i <= maxNum; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
System.out.print("*");
System.out.print(i);
System.out.print("=");
System.out.print(i * j);
System.out.print("\t");
}
System.out.println();
}
}
public static void main(String[] args) {
System.out.print("录入一个整数:");
Scanner input = new Scanner(System.in);
// 没有考虑判断输入的是否是整数。
int maxNum = input.nextInt();
new hello(maxNum).print();
}
} 展开
2个回答
展开全部
用的IDE是Eclipse么?建议用加断点自己跟踪调试,看看循环里的每一步都干了些什么,如下图:
你的第一段代码:
public static void main(String args[]) {
int layer = 4;
for (int i = 1; i <= layer; i++) {
for (int k = 1; k <= layer - i; k++) {
System.out.print(" ");
}
for (int j = 1; j <= (i - 1) * 2 + 1; j++) {
System.out.print("*");
}
System.out.println();
}
int layer1 = 3;
for (int i = 1; i <= layer1; i++) {
for (int k = 1; k <= (layer1 - i) + 1; k++) {
System.out.print(" ");
}
//这里j++,j会越来越大,死循环了
for (int j = 5; j >= (i + 1) * 2 + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
第二段代码:
public class Hello{
public static void main(String args[]) {
int i1, j1;
for (i1 = 1; i1 <= 7; i1++) {
for (j1 = 1; j1 <= 7; j1++) {
//循环体空,不知道你想干什么
}
if (j1 + i1 == 5 || j1 - i1 == 3 || i1 - j1 == 3 || i1 + j1 == 11) {
System.out.print("*");
}
else {
System.out.print("");
}
}
System.out.println();
}
}
第三段代码:
import java.util.Scanner;
public class Hello{
private int maxNum;
public Hello(int maxNum) {
this.maxNum = maxNum;
}
public void print() {
//第一层for循环,打印maxNum行
for (int i = 1; i <= maxNum; i++) {
//第二层for循环,每次执行i次
for (int j = 1; j <= i; j++) {
System.out.print(j);
System.out.print("*");
System.out.print(i);
System.out.print("=");
System.out.print(i * j);
System.out.print("\t");
}
System.out.println();
}
}
public static void main(String[] args) {
System.out.print("录入一个整数:");
Scanner input = new Scanner(System.in);
// 没有考虑判断输入的是否是整数。
int maxNum = input.nextInt();
new Hello(maxNum).print();
}
}
在网上搜搜Eclipse怎么断点调试,你就可以看到每次循环都干了什么,一一给你解释太麻烦了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询