将对象变量放入扫描仪中

huangapple go评论87阅读模式
英文:

Placing object variables in scanner

问题

I'm quite new to java so any input or help would be greatly appreciated. I'm trying to create a program that asks a user for a bunch of inputs (such as name, registration, colour, # of trips and odometer reading) for a `Car` class. I've created a car class, created an object of that class called `carSample` and given it the same variable names from my main method where I use a scanner to ask for name, registration etc...

However, this does not work and I receive the error:

>TestCar.java:8: error: ';' expected
String carSample.name = input.nextLine();

Here is my code:

public static void main(String[] args){

	Scanner input = new Scanner(System.in);

	System.out.print("Input name: ");

	String carSample.name = input.nextLine();

	System.out.print("Input registration: ");

	String carSample.registration = input.nextLine();

	System.out.print("Input colour: ");

	String carSample.colour = input.nextLine();

	System.out.print("Input trips: ");
	
	int carSample.numberOfTrips = input.nextInt();
	
	for (int i = 0; i < numberOfTrips; i++) {
		System.out.print("Odometer reading " + (i + 1) + ": ");
		int odometerReading = input.nextInt();
	}

	car carSample = new car(); // Creates object of class Car
}

class car {
	String name;
	String registration;
	String colour;
	int numberOfTrips;
	double odometerReading;
}

Any help would be greatly appreciated, thank you !
英文:

I'm quite new to java so any input or help would be greatly appreciated. I'm trying to create a program that asks a user for a bunch of inputs (such as name, registration, colour, # of trips and odometer reading) for a Car class. I've created a car class, created an object of that class called carSample and given it the same variable names from my main method where i use a scanner to ask for name,registration etc...

However, this does not work and i receive the error:

>TestCar.java:8: error: ';' expected
String carSample.name = input.nextLine();

Here is my code:

public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print(&quot;Input name: &quot;);
String carSample.name = input.nextLine();
System.out.print(&quot;Input registration: &quot;);
String carSample.registration = input.nextLine();
System.out.print(&quot;Input colour: &quot;);
String carSample.colour = input.nextLine();
System.out.print(&quot;Input trips: &quot;);
int carSample.numberOfTrips = input.nextInt();
for (int i = 0; i &lt; numberOfTrips; i++) {
System.out.print(&quot;Odometer reading &quot; + (i + 1) + &quot;: &quot;);
int odometerReading = input.nextInt();
}
car carSample = new car(); // Creates object of class Car
}
class car {
String name;
String registration;
String colour;
int numberOfTrips;
double odometerReading;
}

Any help would be greatly appreciated, thank you !

答案1

得分: 1

...

Car carSample = new Car(); // 创建Car类的对象
...
carSample.name = input.nextLine();
...
carSample.registration = input.nextLine();
...
carSample.numberOfTrips = input.nextLine();
...
carSample.name = input.nextLine();

最后,在for循环中使用numberOfTrips时,需要使用car.numberOfTrips进行访问。

英文:
String carSample.name = input.nextLine();
  1. carSample is declared in the car class. When you assign a value to a variable that is already declared, you don't have to specify the type. (it breaks when you do)
  2. When you assign a value to a variable in an object, the object has to have been created first. Move your creation of the car object to the top of the program.
...

Car carSample = new Car(); // Creates object of class Car
...
carSample.name = input.nextLine();
...
carSample.registration = input.nextLine();
...
carSample.numberOfTrips = input.nextLine();
...
carSample.name = input.nextLine();

Finally, when you use numberOfTrips in the for loop, you need to access it with car.numberofTrips.

答案2

得分: 1

import java.util.*;

class Test {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        Car carSample = new Car(); // Creates object of class Car
        System.out.print("Input name: ");

        carSample.name = input.nextLine();

        System.out.print("Input registration: ");

        carSample.registration = input.nextLine();

        System.out.print("Input colour: ");

        carSample.colour = input.nextLine();

        System.out.print("Input trips: ");

        carSample.numberOfTrips = input.nextInt();

        for (int i = 0; i < carSample.numberOfTrips; i++) {
            System.out.print("Odometer reading " + (i + 1) + ": ");
            int odometerReading = input.nextInt();
        }
    }
}

class Car {
    String name;
    String registration;
    String colour;
    int numberOfTrips;
    double odometerReading;
}
英文:
`import java.util.*;
class Test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
car carSample = new car(); // Creates object of class Car
System.out.print(&quot;Input name: &quot;);
carSample.name = input.nextLine();
System.out.print(&quot;Input registration: &quot;);
carSample.registration = input.nextLine();
System.out.print(&quot;Input colour: &quot;);
carSample.colour = input.nextLine();
System.out.print(&quot;Input trips: &quot;);
carSample.numberOfTrips = input.nextInt();
for (int i = 0; i &lt; carSample.numberOfTrips; i++) {
System.out.print(&quot;Odometer reading &quot; + (i + 1) + &quot;: &quot;);
int odometerReading = input.nextInt();
}
}
}
class car {
String name;
String registration;
String colour;
int numberOfTrips;
double odometerReading;
}`

Try this out ... this should work ...

答案3

得分: 1

我将numberOfTrips和odometerReading移到了CarTripHistory类中。看一下:

public static void main(String[] args){
    Car carSample = new Car();

    Scanner input = new Scanner(System.in);

    System.out.print("输入名称:");

    carSample.name = input.nextLine();

    System.out.print("输入注册信息:");

    carSample.registration = input.nextLine();

    System.out.print("输入颜色:");

    carSample.colour = input.nextLine();
    
    CarTripHistory carTripHistory = new CarTripHistory();

    System.out.print("输入行程次数:");

    carTripHistory.numberOfTrips = input.nextInt();
    carTripHistory.odometerReading = new double[carTripHistory.numberOfTrips];

    for (int i = 0; i < carTripHistory.numberOfTrips; i++) {
        System.out.print("里程表读数 " + (i + 1) + ":");
        carTripHistory.odometerReading[i] = input.nextInt();
    }

    carSample.carTripHistory = carTripHistory;
}

class Car {
    String name;
    String registration;
    String colour;
    CarTripHistory carTripHistory;
}

class CarTripHistory{
    int numberOfTrips;
    double[] odometerReading;
}
英文:

I moved numberOfTrips and odometerReading into CarTripHistory class. Check it out:

public static void main(String[] args){
Car carSample = new Car();
Scanner input = new Scanner(System.in);
System.out.print(&quot;Input name: &quot;);
carSample.name = input.nextLine();
System.out.print(&quot;Input registration: &quot;);
carSample.registration = input.nextLine();
System.out.print(&quot;Input colour: &quot;);
carSample.colour = input.nextLine();
CarTripHistory carTripHistory = new CarTripHistory();
System.out.print(&quot;Input trips: &quot;);
carTripHistory.numberOfTrips = input.nextInt();
carTripHistory.odometerReading = new double[carTripHistory.numberOfTrips];
for (int i = 0; i &lt; carTripHistory.numberOfTrips; i++) {
System.out.print(&quot;Odometer reading &quot; + (i + 1) + &quot;: &quot;);
carTripHistory.odometerReading[i] = input.nextInt();
}
carSample.carTripHistory = carTripHistory;
}
class Car {
String name;
String registration;
String colour;
CarTripHistory carTripHistory;
}
class CarTripHistory{
int numberOfTrips;
double[] odometerReading;
}

答案4

得分: 0

你需要创建对象的实例,想象你有两辆车,分别是 ab
并且你希这些车拥有不同的属性,每次调用 car a = new car() 时,你都在创建一辆新车,所有属性的值都为 null。然后你可以使用:

 a.name = input.nextLine();

通过这样做,你为车辆 a 的属性 name 赋予了一个值,诸如此类...

虽然这并不是最佳的做法,因为你不应该直接从车辆类中直接使用属性。相反,你应该创建一些构造函数和设置与获取方法。

英文:

You need to create the instance of the object, imagine you have to cars a and b.
And you want them to have different properties, each time you call car a = new car() you are creating a new car, with all the attributes null. And then you can use

 a.name = input.nextLine();

So with this you are giving the car a a value to the attribute name, and so on...

Although this is not the best way, because you shouldn't use the attributes directly from the car class on the other class. Instead you should create some constructors and set & get methods.

huangapple
  • 本文由 发表于 2020年4月7日 16:13:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/61075577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定