Java 将用户整数输入替换为变量,使其递增或递减

Java 将用户整数输入替换为变量,使其递增或递减,java,Java,正如标题所说,我试图用用户输入的内容替换列出的I变量,这样,当提示时,它将根据他们为基整数输入的内容递增或递减。基本上,我希望for循环以输入的速度整数的增量为基础 我试着输入各种东西,但很明显我不知道我在做什么,也不清楚这些东西是什么。另外,我不知道编写Java的标准是什么,所以我为代码的草率表示歉意 以下是我的实际目标: 2.汽车等级 编写一个名为car的类,该类具有以下字段: 年鉴。yearModel字段是一个整数,用于保存汽车的年份模型。 玛卡。make字段引用保存汽车品牌的字符串对象。

正如标题所说,我试图用用户输入的内容替换列出的I变量,这样,当提示时,它将根据他们为基整数输入的内容递增或递减。基本上,我希望for循环以输入的速度整数的增量为基础

我试着输入各种东西,但很明显我不知道我在做什么,也不清楚这些东西是什么。另外,我不知道编写Java的标准是什么,所以我为代码的草率表示歉意

以下是我的实际目标:

2.汽车等级 编写一个名为car的类,该类具有以下字段:

年鉴。yearModel字段是一个整数,用于保存汽车的年份模型。 玛卡。make字段引用保存汽车品牌的字符串对象。 速度“速度”字段是一个整数,用于保持汽车的当前速度。 此外,该类应该具有以下构造函数和其他方法

构造器。构造师应该接受汽车的年份模型,并将其作为参数。这些值应指定给对象的yearModel和make字段。构造函数还应将0指定给速度字段

存取器。适当的访问器方法应该获取存储在对象的yearModel、make和speed字段中的值

加速。每次调用accelerate方法时,该方法应在速度字段中添加5

刹车。每次调用制动方法时,应从速度场中减去5

在创建汽车对象的程序中演示该类,然后调用accelerate方法五次。每次调用accelerate方法后,获取汽车的当前速度并显示它。然后调用制动方法五次。每次调用制动方法后,获取车辆的当前速度并显示它


编辑:我整理了一些东西,并试图进一步澄清它。

好吧,所以我算是弄明白了。我创建了两个类,然后重新开始这类工作

这是我的第一节课:

public class carclass1 {//Start of Class

    private int yearModel; //an int that holds the car's year model
    private String make; //references a string object that holds the make of the car
    private int speed; //an int that holds the car's speed.

    public int getYearModel() {
        return yearModel;
    }

    public String getMake(){ 
        return make; //This is the field of the particular object
    }

    public int getSpeed(){ 
        return speed;
    }

    public void accelerate() {
        speed = speed + 5;
    }

    public void brake() {
        speed = speed - 5;
    }

    Scanner input = new Scanner(System.in);

    public carclass1( int yearModelGiven, String makeGiven ){
        yearModel = yearModelGiven;
        make = makeGiven;
        System.out.println("Please enter in the initial speed of the car:");
        speed = input.nextInt();
    }
}
二等舱:

public class carclass2{

    public static void main(String [] args ) { //this is a main method

        Scanner input = new Scanner( System.in );  

        System.out.println("What is the vehicle year: ");

        int carYear; 
        carYear = input.nextInt();

        System.out.println( "The vehicle year is "+ carYear + " (type a number on the` next prompt to proceed");
        carYear = input.nextInt();

        System.out.println( "What is the vehicle make: ");

        input.nextLine(); /*The purpose for this is to skip to the next prompt
        * I couldn't find another way around why my code won't work without it.*/
        String carMake1;
        carMake1 = input.nextLine();

        System.out.println( "The vehicle make is "+ carMake1);

        carclass1 car1 = new carclass1( carYear, "Honda" );

        System.out.println( "Accelerating the vehicle now:" );  
        for( int seconds = 1; seconds <= 5; seconds++ ) {
            car1.accelerate();
            System.out.println( "The current speed of the" + " " + car1.getYearModel() + " " + car1.getMake() + " is " +
                      car1.getSpeed() + " km/h" );
        }

        // This is now the braking method
        System.out.println( "Braking the vehicle now:" );
        for (int seconds = 1; seconds <= 5; seconds++ ) {
             car1.brake();
             System.out.println( "The current speed of the" + " " + car1.getYearModel() + " " + car1.getMake() + " is " +
                      car1.getSpeed() + " km/h" );
        }
    }
}

我稍微修改了您的代码,并在其中添加了一些解释

下一行将扫描包含换行符的行。如果使用“下一步”或“下一步”,扫描仪将不会读取换行符。这似乎是初学者普遍感到困惑的原因

我在Car类中更改了构造函数,因此它不会停止并等待用户输入。相反,我将扫描速度输入放在了扫描其他信息的位置。我还把车换成了两个车手。一个是在没有速度的情况下可以工作的,另一个是需要速度以及年份和车型

import java.util.Scanner;

class Car { // Class names start with uppercase

    private int yearModel; //an int that holds the car's year model
    private String make; //references a string object that holds the make of the car
    private int speed; //an int that holds the car's speed.

    public int getYearModel() {
        return yearModel;
    }

    public String getMake(){ 
        return make; //This is the field of the particular object
    }

    public int getSpeed(){ 
        return speed;
    }

    public void accelerate() {
        speed = speed + 5;
    }

    public void brake() {
        speed = speed - 5;
    }

//    Scanner input = new Scanner(System.in); // Avoid IO-dependency in constuctor

    public Car(int yearModelGiven, String makeGiven){
        this.yearModel = yearModelGiven;
        this.make = makeGiven;
        this.speed = 0;
//        System.out.println("Please enter in the initial speed of the car:");
//        speed = input.nextInt();
    }

    // Overloaded constructor
    public Car(int yearModelGiven, String makeGiven, int speed){ // extra parameter
        this.yearModel = yearModelGiven;
        this.make = makeGiven;
        this.speed = speed;
    }

}

public class Application{

    public static void main(String [] args ) { //this is a main method

        Scanner input = new Scanner( System.in );  

        System.out.println("What is the vehicle year: ");
        int carYear; 
        carYear = input.nextInt();
        input.nextLine(); // This line will scan the rest of the line after the number: The linefeed.
        System.out.println("The vehicle year is " + carYear);

        System.out.println("What is the vehicle make: ");
        String carMake;
//        carMake = input.nextLine(); // nextLine() scans the rest of the line including the linefeed
// The above line will do the same as the below two:
        carMake = input.next();
        input.nextLine();
        System.out.println("The vehicle make is "+ carMake);

        System.out.println("Please enter in the initial speed of the car:");
        int speed = input.nextInt();
        input.nextLine(); // This line will scan the rest of the line after the number: The linefeed.

//        Car car1 = new Car(carYear, carMake);  // will give it default speed 0
        Car car1 = new Car(carYear, carMake, speed); // will give it the speed entered by user.

        System.out.println( "Accelerating the vehicle now:" );  
        for( int seconds = 1; seconds <= 5; seconds++ ) {
            car1.accelerate();
            System.out.println( "The current speed of the" + " " + car1.getYearModel() + " " + car1.getMake() + " is " +
                      car1.getSpeed() + " km/h" );
        }

        // This is now the braking method
        System.out.println( "Braking the vehicle now:" );
        for (int seconds = 1; seconds <= 5; seconds++ ) {
             car1.brake();
             System.out.println( "The current speed of the" + " " + car1.getYearModel() + " " + car1.getMake() + " is " +
                      car1.getSpeed() + " km/h" );
        }
    }
}

我不太明白你的问题。你能给出示例输入和输出吗?循环的作用是什么?为什么这两条循环线相同?是否要更改调用accelerate/brake方法的次数,或者更改一次调用accelerate/brake将应用于速度的增量/减量偏移量?您可能需要删除帖子顶部附近的循环。他们什么都不做,这让人困惑。@ModusTollens这是输入和输出。真的一年又一年都不要紧,不过我想速度要紧。车辆当前状态:车年:[DrJava输入框]车辆制造:[DrJava输入框]速度:[DrJava输入框]加速5次后,制动5次后速度=25 km/h,速度=0@Progman我想要从int speed=input.nextInt输入的速度值;以用户的节为基础。目前它以5的增量增加,从0开始,我现在无法更改。我希望速度是基本值,因此如果它从20开始,它将从那里以5的增量增加,而不是从0开始。您应该命名表示汽车的类,并且测试程序应该是一个单独的类。类名应该以大写开头,以免与以小写开头的变量名混淆。此外,读取基本速度的代码不应添加到新的carclass1构造函数中。该类的任务不是读取用户输入。在该类之外执行此操作,并将基本速度作为新参数提供给构造器,如new CarclassCaryear、Honda、baseSpeed;,其中baseSpeed包含用户之前询问的基本速度。非常感谢!事实上,在这段时间里,我去了学校的一位家庭教师那里,他给我看了完全相同的东西。我们的课文和我的指导老师在下一次的活动中从未讨论过这个问题。这段时间我们一直在做nextLine,这对我帮助很大。@mightymorphinParkRanger我很高兴这对你有帮助。你可以选择接受一个最适合你的问题的答案,或者你认为是最好的答案或者对你帮助最大的答案。使用当前灰色复选框 k、 看。标记自己的答案没有什么错。
import java.util.Scanner;

class Car { // Class names start with uppercase

    private int yearModel; //an int that holds the car's year model
    private String make; //references a string object that holds the make of the car
    private int speed; //an int that holds the car's speed.

    public int getYearModel() {
        return yearModel;
    }

    public String getMake(){ 
        return make; //This is the field of the particular object
    }

    public int getSpeed(){ 
        return speed;
    }

    public void accelerate() {
        speed = speed + 5;
    }

    public void brake() {
        speed = speed - 5;
    }

//    Scanner input = new Scanner(System.in); // Avoid IO-dependency in constuctor

    public Car(int yearModelGiven, String makeGiven){
        this.yearModel = yearModelGiven;
        this.make = makeGiven;
        this.speed = 0;
//        System.out.println("Please enter in the initial speed of the car:");
//        speed = input.nextInt();
    }

    // Overloaded constructor
    public Car(int yearModelGiven, String makeGiven, int speed){ // extra parameter
        this.yearModel = yearModelGiven;
        this.make = makeGiven;
        this.speed = speed;
    }

}

public class Application{

    public static void main(String [] args ) { //this is a main method

        Scanner input = new Scanner( System.in );  

        System.out.println("What is the vehicle year: ");
        int carYear; 
        carYear = input.nextInt();
        input.nextLine(); // This line will scan the rest of the line after the number: The linefeed.
        System.out.println("The vehicle year is " + carYear);

        System.out.println("What is the vehicle make: ");
        String carMake;
//        carMake = input.nextLine(); // nextLine() scans the rest of the line including the linefeed
// The above line will do the same as the below two:
        carMake = input.next();
        input.nextLine();
        System.out.println("The vehicle make is "+ carMake);

        System.out.println("Please enter in the initial speed of the car:");
        int speed = input.nextInt();
        input.nextLine(); // This line will scan the rest of the line after the number: The linefeed.

//        Car car1 = new Car(carYear, carMake);  // will give it default speed 0
        Car car1 = new Car(carYear, carMake, speed); // will give it the speed entered by user.

        System.out.println( "Accelerating the vehicle now:" );  
        for( int seconds = 1; seconds <= 5; seconds++ ) {
            car1.accelerate();
            System.out.println( "The current speed of the" + " " + car1.getYearModel() + " " + car1.getMake() + " is " +
                      car1.getSpeed() + " km/h" );
        }

        // This is now the braking method
        System.out.println( "Braking the vehicle now:" );
        for (int seconds = 1; seconds <= 5; seconds++ ) {
             car1.brake();
             System.out.println( "The current speed of the" + " " + car1.getYearModel() + " " + car1.getMake() + " is " +
                      car1.getSpeed() + " km/h" );
        }
    }
}