下面是我做好的功课,老师说我里面出问题...但又不肯解释到底那出问题.....请各位帮我看看因该如何改正,可以的话因该如何完善他呢??谢谢 /*
 * Main.java
 *
 * Java application
 *
 * Created on 27-09-2008 20:16 PM
 */
 import java.io.*;
 
public class Main
{
        
public static void main(String args[]) throws Exception {
java.io.BufferedReader buf;
buf = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
System.out.print("Enter the first number:");
int x = Integer.parseInt(buf.readLine()); java.io.BufferedReader buf1;
buf1=new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
System.out.print("Please enter an operator(+,-,*,/):");
String operator = buf1.readLine(); java.io.BufferedReader buf2;
buf2 = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
System.out.print("Enter the second number:");
int y = Integer.parseInt(buf2.readLine());

if (operator.equals("+")){System.out.print(x+"+"+y+"="+(x+=y));}
if (operator.equals("-")){System.out.print(x+"-"+y+"="+(x-=y));}
if (operator.equals("*")){System.out.print(x+"*"+y+"="+(x*=y));}
if (operator.equals("/"))
{if(y==0){
System.out.print("Error!Divisor cannot be ZERO");}
if(y!=0)
{System.out.print(x+"/"+y+"="+(x/=y));}
if (operator.equals("#")){System.out.print("Error!Invalid operator'#'");}

}
}/*
 * Main.java
 *
 * Main Java application
 *
 * Created on 28-09-2008 02:17 PM
 */import java.text.*;
import java.io.*;
import java.lang.*;class Ball{
//attribute
double radius;
String name;
//constructor
public Ball(){
         radius = 0;
         name = "";
}
public Ball(double r,String n){
         radius = r;
         name = n;
}
//method getRadius
public double getRadius() { 
return radius; 
}
//method getName                                                                       
public String getName() { 
         return name; 

//method bigball
public void bigball(){
radius =2;
}
//method smallball
public void smallball(){
radius =1;
}
//method Basketball
public void Basketball(){
name = "basketball";
}
}
public class Main{
public static void main(String[] args) throws IOException{
Ball a1;
a1 = new Ball(2 ,"basketball");
a1.getRadius();
a1.getName();
a1.bigball();
a1.Basketball();
Ball a2;
a2 = new Ball(1,"football");
a2.getRadius();
a2.getName();
a2.smallball();
}
}

解决方案 »

  1.   

    请楼主仔细检查一下,你两个题目均犯了同一个错误就是类名,
    Main是java中保留字段是不能够做类名的,请楼主以后一定要注意类名的设置.而且,提醒楼主,对于第一个题目中你已经把io包导入进来了,为什么还java.io.BufferedReader,这样不麻烦吗?还有对第二个题目中,你的
    a1.bigball(); 
    a1.Basketball(); a2.smallball(); 
    豪无意义,因为你在a1,a2初始化时就已经设置为相应的属性了,还请楼主仔细考虑!
      

  2.   

    我没发现什么严重的问题
    1 类名可以是Main 没关系
    2 你既然是三个类,就别把类名重复了,至少叫 Main1 Main2 也好啊!
    3 Ball 类,你有get方法,为何没有对应的set方法呢? 也许不需要,这个倒没啥关系。
    4 你的加减乘除的代码,唯一要注意的,就是如果用户没有输入数字,或者根本啥也没输入就按了回车,你要进行异常处理,然后用户再次输入。
      

  3.   


    顶 ,另外在main方法还使用throws是很不厚道的做法哦,呵呵