首页 > java > Java的。用户的输入通过实例变量在主类中工作,但在其他类中则不然

Java的。用户的输入通过实例变量在主类中工作,但在其他类中则不然 (Java. User's input is working in main class through instance variable but not in other class)

问题

Java的。用户的输入通过实例变量在主类中工作,但在其他类中则不然

用户在songtestdrive主类中工作时输入类歌的song1变量的值,而不是在录音类中输入。在Recording类中,它将用户的值打印为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)实例和song您在recording(song_recording)中创建的实例是不同的。

您可以将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类构造函数,如果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命名约定来命名类名。类名应以大写字母开头...... SongRecording...

变量名必须遵循驼峰式样式 - songRecording

问题

Java. User's input is working in main class through instance variable but not in other class

Users input value of song1 variable of class song in working in songtestdrive main class but not in the recording class. In Recording class it prints the user's value as 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);
}}

解决方法

The song(rock) instance you create in main and the song instance you create in recording (song_recording) are different.

You can pass the rock variable to the recording method

public void recording(Song song)
{  
    //song song_recording = new song(); <-- Remove this
    System.out.println("Do you want to record songtype "+ song.song1);
}

The caller becomes

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");
}

Or the recording class constructor if a recording instance is always related to one song.

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);
    }
}

The caller becomes

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");
}

Note: Follow Java naming conventions to name class names. Class names should start with an uppercase letter... Song, Recording...

Variable names must follow camel case style - songRecording

相似信息