注册 登录
编程论坛 JAVA论坛

在主函数中调用Scanner函数出错?求各位给看一下

EMMMM 发布于 2017-10-11 18:21, 3303 次点击
代码如下:
public class JavaApplication5 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
 Scanner input = new Scanner(System.in);
        Tank tank1 = new Tank();  
        Tank tank2 = new Tank();         
        tank1.setBulletAmount(10);  
        tank2.setBulletAmount(1);   
        double tank1.speedUp =input.nextDouble();   
        double tank2.speedUp =input.nextDouble();  
这里ide提示我需要‘;’我的分号是英文的而且写了啊,下面的一句一样是这个错误,不明白为什么错了,求各位指点一下。
        tank1.fire();  
        tank2.fire();         
        System.out.println("tank1目前的速度:"+tank1.getSpeed());               
        System.out.println("tank2目前的速度:"+tank2.getSpeed());  
        System.out.println("tank1现有炮弹数量:"+tank1.getBulletAmount());  
        System.out.println("tank2现有炮弹数量:"+tank2.getBulletAmount());
        double tank1.speedUp =input.nextDouble();               
        double tank2.speedUp =input.nextDouble();
        tank1.fire();  
        tank2.fire();      
        System.out.println("tank1目前的速度:"+tank1.getSpeed());               
        System.out.println("tank2目前的速度:"+tank2.getSpeed());
        System.out.println("tank1的炮弹数量:"+tank1.getBulletAmount());        
        System.out.println("tank2的炮弹数量:"+tank2.getBulletAmount());  
    }
}
 class Tank{
    double speed;        
    int bulletAmount;
    void speedUp(int s){
       speed = speed + s;   }
    void speedDown(int d){  
       if(speed-d >= 0)
           speed = speed -d;
       else
           speed = 0; }  
    void setBulletAmount(int m){  
       bulletAmount = m; }
    int getBulletAmount(){        
       return bulletAmount; }
    double getSpeed(){            
       return speed; }
    void fire(){               
       if(bulletAmount >= 1){
           bulletAmount = bulletAmount -1;
           System.out.println("打出一发炮弹");       }
       else {
           System.out.println("没有炮弹了");          }
    }  
9 回复
#2
王小贱20162017-10-11 21:35
class Tank{
    double speed;        
    int bulletAmount;
    void speedUp(int s){//这个地方的返回类型有问题呀应该用double吧

       speed = speed + s;   }
    void speedDown(int d){  
       if(speed-d >= 0)
           speed = speed -d;
       else
           speed = 0; }  
    void setBulletAmount(int m){  
       bulletAmount = m; }
    int getBulletAmount(){        
       return bulletAmount; }
    double getSpeed(){            
       return speed; }
    void fire(){               
       if(bulletAmount >= 1){
           bulletAmount = bulletAmount -1;
           System.out.println("打出一发炮弹");       }
       else {
           System.out.println("没有炮弹了");          }
    }   
#3
calix2017-10-11 21:39
double tank1.speedUp =input.nextDouble();
给对象属性赋值不需要声明类型
tank1.speedUp = input.nextDouble();
#4
EMMMM2017-10-11 22:20
回复 2楼 王小贱2016
好,谢谢您了,这里也错了,没看出来...
#5
EMMMM2017-10-11 22:22
回复 3楼 calix
谢谢您
#6
等候小小2017-10-11 22:25
是int类型,不应该是double类型
#7
等候小小2017-10-11 22:26
tank1.speedUp(input.nextInt())
#8
EMMMM2017-10-12 19:21
回复 3楼 calix
您好还是不行啊,我改成了tank1.speedUp=input.nextDouble(); 他提示找不到符号(speedUp)
#9
calix2017-10-12 19:44
不好意思,没仔细看,你的speedUp不是属性,是个方法,7楼的写法可以
public class JavaApplication5 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Tank tank1 = new Tank();
        Tank tank2 = new Tank();
        tank1.setBulletAmount(10);
        tank2.setBulletAmount(1);
        tank1.speedUp(input.nextDouble());
        tank2.speedUp(input.nextDouble());
        tank1.fire();
        tank2.fire();
        System.out.println("tank1目前的速度:" + tank1.getSpeed());
        System.out.println("tank2目前的速度:" + tank2.getSpeed());
        System.out.println("tank1现有炮弹数量:" + tank1.getBulletAmount());
        System.out.println("tank2现有炮弹数量:" + tank2.getBulletAmount());
        tank1.speedUp(input.nextDouble());
        tank2.speedUp(input.nextDouble());
        tank1.fire();
        tank2.fire();
        System.out.println("tank1目前的速度:" + tank1.getSpeed());
        System.out.println("tank2目前的速度:" + tank2.getSpeed());
        System.out.println("tank1的炮弹数量:" + tank1.getBulletAmount());
        System.out.println("tank2的炮弹数量:" + tank2.getBulletAmount());
    }
}

class Tank {
    private double speed;
    private int bulletAmount;

    void speedUp(double s) {
        speed = speed + s;
    }

    void speedDown(int d) {
        if (speed - d >= 0)
            speed = speed - d;
        else
            speed = 0;
    }

    void setBulletAmount(int m) {
        bulletAmount = m;
    }

    int getBulletAmount() {
        return bulletAmount;
    }

    double getSpeed() {
        return speed;
    }

    void fire() {
        if (bulletAmount >= 1) {
            bulletAmount = bulletAmount - 1;
            System.out.println("打出一发炮弹");
        } else {
            System.out.println("没有炮弹了");
        }
    }
}
#10
kingpc5202017-10-13 14:05
666666
1