标题:在主函数中调用Scanner函数出错?求各位给看一下
只看楼主
EMMMM
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2017-9-16
结帖率:75%
已结贴  问题点数:20 回复次数:9 
在主函数中调用Scanner函数出错?求各位给看一下
代码如下:
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("没有炮弹了");          }
    }  
搜索更多相关主题的帖子: void System double out println 
2017-10-11 18:21
王小贱2016
Rank: 1
等 级:新手上路
帖 子:21
专家分:9
注 册:2016-4-23
得分:5 
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("没有炮弹了");          }
    }   
2017-10-11 21:35
calix
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:28
帖 子:249
专家分:1442
注 册:2015-5-4
得分:15 
double tank1.speedUp =input.nextDouble();
给对象属性赋值不需要声明类型
tank1.speedUp = input.nextDouble();
2017-10-11 21:39
EMMMM
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2017-9-16
得分:0 
回复 2楼 王小贱2016
好,谢谢您了,这里也错了,没看出来...
2017-10-11 22:20
EMMMM
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2017-9-16
得分:0 
回复 3楼 calix
谢谢您
2017-10-11 22:22
等候小小
Rank: 1
等 级:新手上路
威 望:1
帖 子:5
专家分:0
注 册:2017-9-5
得分:0 
是int类型,不应该是double类型
2017-10-11 22:25
等候小小
Rank: 1
等 级:新手上路
威 望:1
帖 子:5
专家分:0
注 册:2017-9-5
得分:0 
tank1.speedUp(input.nextInt())
2017-10-11 22:26
EMMMM
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2017-9-16
得分:0 
回复 3楼 calix
您好还是不行啊,我改成了tank1.speedUp=input.nextDouble(); 他提示找不到符号(speedUp)
2017-10-12 19:21
calix
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:28
帖 子:249
专家分:1442
注 册:2015-5-4
得分:0 
不好意思,没仔细看,你的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("没有炮弹了");
        }
    }
}
2017-10-12 19:44
kingpc520
Rank: 3Rank: 3
等 级:论坛游侠
威 望:7
帖 子:43
专家分:178
注 册:2017-9-16
得分:0 
666666
2017-10-13 14:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-481290-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.835075 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved