import java.io.BufferedReader; import java.io.InputStreamReader;Javaで入力変数に変数を設定するにはどうすればよいですか?
パブリッククラスProblem3_1 {
//complete this class, called Problem3_1, with the following items:
//1. Declare four attributes, name, age, height, and weight of types String and int-s.
//Write a constructor for this class that initializes ONLY the name, age, and height to three incoming arguments,
//and sets the weight to always be -1 (the latter is not an incoming argument).
String name;
int age;
int height;
int weight;
Address address;
public Problem3_1(String name, int age, int height) {
this.name = name;
this.age = age;
this.height = height;
weight = -1;
}
void setAddress(int number, String street) {
address = new Address(number, street);
}
//2. Imagine there is a class called Address that you have access to (it's below).
//Its constructor takes an integer street number and a String street. Add an attribute called address to
//the Problem3_1 class, and create a method called setAddress that sets the attribute to the incoming argument.
public static class Address{
int number;
String street;
public Address(int number, String street){
this.number = number;
this.street = street;
}
}
}
BELOWテンプレートは、なぜコードは、私が間違った場所にあるこの空の情報を持っていると言っている// COMPLETE?正しい場所はどこですか?それとも、「適用」できないのですか?
PS:私は取得エラー:
Problem3_1.java:82: setAddress(int,java.lang.String) in Problem3_1 cannot be applied to (Problem3_1.Address) p.setAddress(a);
テストケース:
public static void main(String[] args){
/*Below are tests that will check if you completed the code above correctly; if
your code doesn't compile, you'll need to fix those errors first.
DO NOT WRITE CODE BELOW THIS POINT
*/
int failed = 0;
Problem3_1 p = new Problem3_1("Jane", 22, 65);
if (p.name.compareTo("Jane") == 0 && p.age == 22 && p.height == 65 && p.weight == -1)
System.out.println("Test 1 passed!");
else{
failed ++;
System.out.println("Please check your code for question 1! ");
}
Address a = new Address(12, "Fairfax Dr");
p.setAddress(a);
if (p.address == a)
System.out.println("Test 2 passed!");
else{
failed ++;
System.out.println("Please check your code for question 2! ");
}
if (failed > 0)
System.out.println(systemCall("Failed"));
else{
System.out.println("GREAT WORK! EVERYTHING PASSED!");
System.out.println(systemCall("Nice"));
}
}
あなたはsetAdress関数のアドレスを忘れてしまいました – Marko