import java.io.*;
public class ClassMain {
public static void main(String[] args) {
Game g = new Game();
}

public class Game {
int[] num = new int[5];
int tmp;
Menu m = null;
public Game() {
while(true) {
this.start();
try {
this.getNum();
this.deal();
m = this.getMenu();
}catch(NumberFormatException e) {
e.printStackTrace();
}
}
}

public void start() {
num[0] = (int)(Math.random()*10);
for (int i=1;i<num.length;i++) {
num[i] = num[i-1]*(int)(Math.pow(2.0,(double)(i-1)));
}
System.out.println("游戏开始!");
for (int i=0;i<num.length-1;i++) {
System.out.println(num[i]);
}
System.out.println("猜猜最后个数是多少?");
}
 
public void getNum() {
  byte[] b = new byte[1024];
  try {
System.in.read(b);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
  String str = new String(b).trim();
  try {
this.tmp = Integer.parseInt(str);
} catch (NumberFormatException e) {
e.printStackTrace();
System.exit(0);
}
 }
 
 public void deal() {
if (tmp == num[4]) {
System.out.println("恭喜你猜对了!");
} else {
System.out.println("对不起你答错了!");
}
}

public Menu getMenu() {
return new Menu();
}
}
public class Menu extends Game {
//以后这个构造方法是不是写不写一样,我觉得应该是
         public Menu() {
while(true) {
this.start();
try {
this.getNum();
this.deal();
}catch(NumberFormatException e) {
e.printStackTrace();
}
}
}

public void start() {
System.out.println("=============================");
System.out.println(" 1, 开始");
System.out.println(" 2, 提示答案");
System.out.println(" 3, 结束");
System.out.println("=============================");
System.out.println("请用户输入选择:");
}

public void deal() {
if (tmp == 1) {
System.out.println("游戏重新开始!");
}else if(tmp == 2) {
System.out.println("正确的答案是"+num[4]);
}else if(tmp == 3) {
System.exit(0);
}
}
}我在Game.java中的public void deal()方法中最后NEW了个Menu() ,可是为什么不调用Menu的构造方法来构造个新的对象,Menu我是从Game继承过来的应该默认调用Game的构造方法啊 
  

解决方案 »

  1.   

    我又改过了,改成在Game.java中的构造方法中最后相当于NEW了个Menu(),可还是没NEW出来.
      

  2.   

    请问你的这些所有的程序都写在Game.java中了吗?
      

  3.   

    我是分开来写了3个java文件的,编译没有问题,运行时就是不调用public Menu()这个构造方法来构造Menu对象
      

  4.   

    我也是初学者,说得不对请大家多指教:
    我看了你的程序,不过你的程序说实话很乱,Game类和Menu类之间应该没有什么关系,Menu类只是显示一个菜单,有一个属性存放选择菜单的索引,而Game类只需要存取这个属性并调用相应的方法就好了。我修改了一下,你看看!//ClassMain.java
    //package game;import java.io.*;public class ClassMain {
    public static void main(String[] args) {
    Game g = new Game();
    }

    -------------------------------------------------------------------------------------
    //Game.java
    //package game;
    import java.io.*;public class Game {
    int[] num = new int[5];
    int tmp;
    Menu m = null;
    public Game() {
    while(true) {
    this.start();
    this.getNum();
    this.deal();
    m= new Menu();
    if(m.getSelect()==3)break;
    if(m.getSelect()==2)System.out.println("正确的答案是"+num[4]);

    }
    } public void start() {
    num[0] = (int)(Math.random()*10);
    for (int i=1;i<num.length;i++) {
    num[i] = num[i-1]*(int)(Math.pow(2.0,(double)(i-1)));
    }
    System.out.println("游戏开始!");
    for (int i=0;i<num.length-1;i++){
    System.out.println(num[i]);
    }
    System.out.println("猜猜最后个数是多少?");
    }
    public void getNum() {
    byte[] b = new byte[1024];
    try {
    System.in.read(b);
    }catch(IOException e) {
    e.printStackTrace();
    System.exit(0);
    }
    String str = new String(b).trim();
    try {
    this.tmp = Integer.parseInt(str);
    }catch (NumberFormatException e) {
    e.printStackTrace();
    System.exit(0);
    }
    }
     

     
    public void deal() {
    if (tmp == num[4]) {
    System.out.println("恭喜你猜对了!");

    else{
    System.out.println("对不起你答错了!");
    }
    }
    }
    -------------------------------------------------------------------------------------
    //Menu.java//package game;
    import java.io.*;public class Menu {
    private int selectMenu;
    public Menu() {
    //while(true) {
    this.startMenu();
    try {
    this.getNum();
    // this.deal();
    }catch(NumberFormatException e) {
    e.printStackTrace();
    }
    //}
    } public void startMenu() {
    System.out.println("=============================");
    System.out.println("1, 开始");
    System.out.println("2, 提示答案");
    System.out.println("3, 结束");
    System.out.println("=============================");
    System.out.println("请用户输入选择:");
    }
    public void getNum() {
    byte[] b = new byte[1024];
    try {
    System.in.read(b);
    }catch(IOException e) {
    e.printStackTrace();
    System.exit(0);
    }
    String str = new String(b).trim();
    try {
    this.selectMenu = Integer.parseInt(str);
    }catch (NumberFormatException e) {
    e.printStackTrace();
    System.exit(0);
    }
    } public int getSelect(){
    return selectMenu;
    }/*
    public void deal() {
    if (selectMenu == 1) {
    System.out.println("游戏重新开始!");
    }
    else if(selectMenu == 2) {
    System.out.println("正确的答案是"+num[4]);
    }
    else if(selectMenu == 3) {
    System.exit(0);
    }
    }
    */
    }