首页 > 试题广场 >

这一页的Java程序代码都代表一份完整的源文件。你的任务是要

[问答题]
这一页的Java程序代码都代表一份完整的源文件。你的任务是要扮演编译器角色并判断哪支程序可以编译通过。如果有问题,哪里需要修改?
A
class Books {
   String title;
   String author;
 }
 class BooksTestDrive {
   public ststic void main{String [] args) { 
     Books [] myBooks = new Books[3];
     int x = 0;
     myBooks[0].title = "The Grapes of Java";
     myBooks[1].title = "The Java Gatsby";
     myBooks[2].title = "The Java Cookbook";
     myBooks[0].author = "bob";
     myBooks[1].author = "sue";
     myBooks[2].author = "ian";
     while ( x < 3 ) {
       System.out.print(myBooks[x].title);
       System.out.print("by");
       System.out.println(myBooks[x].author);
       x = x + 1;
     }
   }
 }
B
class Hobbits {
   String name;
   public static void main(String [] args) {
     Hobbits [] h = new Hobbits[3];
     int z = 0;
     while (z < 4) {
       z = z + 1;
       h[z] = new Hobbits();
       h[z].name = "bilbo";
       if (z == 1) {
         h[z].name = "frodo";
       }
       if (z == 2) {
         h[z].name = "sam";
       }
       System.out.print(h[z].name + " is a ");
       System.out.println("good Hobbit name");
     }
   }
 }

class Hobbits {
	   String name;
	   public static void main(String [] args) {
	     Hobbits [] h = new Hobbits[3];
	     int z = 0;
	     while (z < 3) {//循环小于4,z=3角标溢出
	       h[z] = new Hobbits();
	       h[z].name = "bilbo";
	       if (z == 1) {
	         h[z].name = "frodo";
	       }
	       if (z == 2) {
	         h[z].name = "sam";
	       }
	       System.out.print(h[z].name + " is a ");
	       System.out.println("good Hobbit name");
	       z = z + 1;//自增需最后执行,否则z=2角标溢出
	     }
	   }
	 }


发表于 2021-05-07 14:20:27 回复(0)
class Books {
   String title;
   String author;
 }
 class BooksTestDrive {
   public ststic void main{String [] args) { 
     Books [] myBooks = new Books[3];
     int x = 0;
//创建对象
    myBooks[0] = new Books();
    myBooks[1] = new Books();
    myBooks[2] = new Books();

     myBooks[0].title = "The Grapes of Java";
     myBooks[1].title = "The Java Gatsby";
     myBooks[2].title = "The Java Cookbook";
     myBooks[0].author = "bob";
     myBooks[1].author = "sue";
     myBooks[2].author = "ian";
     while ( x < 3 ) {
       System.out.print(myBooks[x].title);
       System.out.print("by");
       System.out.println(myBooks[x].author);
       x = x + 1;
     }
   }
 }
发表于 2021-05-07 10:22:00 回复(0)