Java。用户的输入通过实例变量在 main class 中起作用,但在其他 class 中不起作用
Java. User's input is working in main class through instance variable but not in other class
Java。用户的输入通过实例变量在 main class 中起作用,但在其他 class
中不起作用
用户在 songtestdrive main class 中输入了 class 歌曲的 song1 变量值,但在录音 class 中却没有。在 Recording class 中,它将用户的值打印为 NULL
import java.util.Scanner;
class songtestdrive{
public static void main(String[] args)
{ song rock = new song();
recording record = new recording();
rock.songtype();
record.recording();
// here song class's user value is working with rock.song1
System.out.println("So you are "+rock.song1+" Music lover");
}}
class song
{ Scanner in = new Scanner(System.in);
String song1;
String songtype()
{ System.out.println("Which type of songs you like");
song1= in.nextLine();
return(song1);
}}
class recording
{
String record,yesno;
public void recording()
{ song song_recording = new song();
// need help here song_recording.song1 is unable to show the user's input instead showing null
System.out.println("Do you want to record songtype "+ song_recording.song1);
}}
您在 main 中创建的 song
(rock
) 实例与您在 recording
(song_recording
) 中创建的 song
实例不同。
您可以将 rock
变量传递给 recording
方法
public void recording(Song song)
{
//song song_recording = new song(); <-- Remove this
System.out.println("Do you want to record songtype "+ song.song1);
}
来电者变为
public static void main(String[] args)
{ song rock = new song();
recording record = new recording();
rock.songtype();
record.recording(rock);
System.out.println("So you are "+rock.song1+" Music lover");
}
或者 recording
class 构造函数,如果 recording
实例总是与一首歌相关。
class recording {
String record,yesno;
Song song;
class recording(Song song) {
this.song = song;
}
public void recording() {
//song song_recording = new song(); <-- Remove this
System.out.println("Do you want to record songtype "+ song.song1);
}
}
来电者变为
public static void main(String[] args)
{ song rock = new song();
recording record = new recording(rock);
rock.songtype();
record.recording();
// here song class's user value is working with rock.song1
System.out.println("So you are "+rock.song1+" Music lover");
}
注意:
按照 Java 命名约定命名 class 个名称。 Class 名称应以大写字母开头...Song
、Recording
...
变量名必须遵循驼峰式大小写 - songRecording
您需要通过添加构造函数将歌曲传递给录音 class:
class recording
{
String record,yesno;
song rock;
public recording(song rock) {
this.rock = rock;
}
public void recording()
{
// need help here song_recording.song1 is unable to show the user's input instead showing null
System.out.println("Do you want to record songtype "+ rock.song1);
}}
并更新您的主要代码:
public static void main(String[] args)
{ song rock = new song();
recording record = new recording(rock);
rock.songtype();
record.recording();
// here song class's user value is working with rock.song1
System.out.println("So you are "+rock.song1+" Music lover");
}}
此代码片段可为您提供所需的输出:
public class songtestdrive{
public static void main(String[] args) throws IOException{
song rock = new song();
//recording record = new recording();
rock.songtype();
// We take the recording object out of the code because it is a different obj.
System.out.println("So you are "+rock.song1+" Music lover");
}}
class song{
Scanner in = new Scanner(System.in);
String song1;
String songtype(){
System.out.println("Which type of songs you like");
song1= in.nextLine();
recording record = new recording();
record.recording(song1);
return(song1);
}}
class recording extends song{
String record, yesno;
public void recording(String song1){
// need help here song_recording.song1 is unable to show the user's input instead showing null
System.out.println("Do you want to record songtype " + song1 + "?");
}}
输出结果如下:
Which type of songs you like
rock
Do you want to record songtype rock?
So you are rock Music lover
您的代码中的问题最初是您创建了一个歌曲对象和一个录音对象,然后您希望录音对象从歌曲对象接收变量 "song1"。然而这是不可能的。我编辑你的代码的方式是通过向它传递一个参数来简单地触发录制对象,它在歌曲 class 中实例化。这样它就有了对 song1 变量的引用。您构建代码的方式,录音对象永远不会知道 song1,这就是为什么它 return "null"。在歌曲 class 中,我们只需在 THERE 创建我们的录音对象,然后将 song1 参数传递给它,以便它知道该变量。这将为您提供所需的输出。
Java。用户的输入通过实例变量在 main class 中起作用,但在其他 class
中不起作用用户在 songtestdrive main class 中输入了 class 歌曲的 song1 变量值,但在录音 class 中却没有。在 Recording class 中,它将用户的值打印为 NULL
import java.util.Scanner;
class songtestdrive{
public static void main(String[] args)
{ song rock = new song();
recording record = new recording();
rock.songtype();
record.recording();
// here song class's user value is working with rock.song1
System.out.println("So you are "+rock.song1+" Music lover");
}}
class song
{ Scanner in = new Scanner(System.in);
String song1;
String songtype()
{ System.out.println("Which type of songs you like");
song1= in.nextLine();
return(song1);
}}
class recording
{
String record,yesno;
public void recording()
{ song song_recording = new song();
// need help here song_recording.song1 is unable to show the user's input instead showing null
System.out.println("Do you want to record songtype "+ song_recording.song1);
}}
您在 main 中创建的 song
(rock
) 实例与您在 recording
(song_recording
) 中创建的 song
实例不同。
您可以将 rock
变量传递给 recording
方法
public void recording(Song song)
{
//song song_recording = new song(); <-- Remove this
System.out.println("Do you want to record songtype "+ song.song1);
}
来电者变为
public static void main(String[] args)
{ song rock = new song();
recording record = new recording();
rock.songtype();
record.recording(rock);
System.out.println("So you are "+rock.song1+" Music lover");
}
或者 recording
class 构造函数,如果 recording
实例总是与一首歌相关。
class recording {
String record,yesno;
Song song;
class recording(Song song) {
this.song = song;
}
public void recording() {
//song song_recording = new song(); <-- Remove this
System.out.println("Do you want to record songtype "+ song.song1);
}
}
来电者变为
public static void main(String[] args)
{ song rock = new song();
recording record = new recording(rock);
rock.songtype();
record.recording();
// here song class's user value is working with rock.song1
System.out.println("So you are "+rock.song1+" Music lover");
}
注意:
按照 Java 命名约定命名 class 个名称。 Class 名称应以大写字母开头...Song
、Recording
...
变量名必须遵循驼峰式大小写 - songRecording
您需要通过添加构造函数将歌曲传递给录音 class:
class recording
{
String record,yesno;
song rock;
public recording(song rock) {
this.rock = rock;
}
public void recording()
{
// need help here song_recording.song1 is unable to show the user's input instead showing null
System.out.println("Do you want to record songtype "+ rock.song1);
}}
并更新您的主要代码:
public static void main(String[] args)
{ song rock = new song();
recording record = new recording(rock);
rock.songtype();
record.recording();
// here song class's user value is working with rock.song1
System.out.println("So you are "+rock.song1+" Music lover");
}}
此代码片段可为您提供所需的输出:
public class songtestdrive{
public static void main(String[] args) throws IOException{
song rock = new song();
//recording record = new recording();
rock.songtype();
// We take the recording object out of the code because it is a different obj.
System.out.println("So you are "+rock.song1+" Music lover");
}}
class song{
Scanner in = new Scanner(System.in);
String song1;
String songtype(){
System.out.println("Which type of songs you like");
song1= in.nextLine();
recording record = new recording();
record.recording(song1);
return(song1);
}}
class recording extends song{
String record, yesno;
public void recording(String song1){
// need help here song_recording.song1 is unable to show the user's input instead showing null
System.out.println("Do you want to record songtype " + song1 + "?");
}}
输出结果如下:
Which type of songs you like
rock
Do you want to record songtype rock?
So you are rock Music lover
您的代码中的问题最初是您创建了一个歌曲对象和一个录音对象,然后您希望录音对象从歌曲对象接收变量 "song1"。然而这是不可能的。我编辑你的代码的方式是通过向它传递一个参数来简单地触发录制对象,它在歌曲 class 中实例化。这样它就有了对 song1 变量的引用。您构建代码的方式,录音对象永远不会知道 song1,这就是为什么它 return "null"。在歌曲 class 中,我们只需在 THERE 创建我们的录音对象,然后将 song1 参数传递给它,以便它知道该变量。这将为您提供所需的输出。