简体   繁体   English

将变量从一个类传递到另一个类

[英]Passing the variable from one class to another

Hello everyone I'm currently creating a simple program. 大家好我正在创建一个简单的程序。 I have 2 classes the first one is SampleReturn and the second one is GetValues. 我有2个类,第一个是SampleReturn,第二个是GetValues。 What I want to happen is that when I enter a name in GetValues class the name I entered will be stored in a variable and later on will be used by the SampleReturn class to display the name. 我想要发生的是,当我在GetValues类中输入名称时,我输入的名称将存储在一个变量中,稍后将由SampleReturn类用于显示名称。 Unfortunately, I can't run the program because it has an error. 不幸的是,我无法运行该程序,因为它有错误。 The code is below please help me with regards to this matter. 代码如下,请帮助我解决此事。 I'm just self studying I really want to learn Java. 我只是自学,我真的很想学习Java。 Thanks! 谢谢! :) :)

Code in GetValues Class: GetValues类中的代码:

import java.util.Scanner;
public class GetValues{
    Scanner inp = new Scanner(System.in);

    public static void wew(){
        System.out.println("Enter name: ");
        String a = inp.nextLine();

        private static String z = a;

        public static String kuhaName(){
            return z;
        }
    }
}

Code in SampleReturn: SampleReturn中的代码:

import java.util.Scanner;
    public class SampleReturn{
        public static void main(String[]args){

        String nameMo = GetValues.kuhaName();

        System.out.print("Your name is: " +nameMo);
    }
}

Your Code should be something like this: 您的代码应该是这样的:

import java.util.Scanner;

public class GetValues
{
    private static Scanner inp = new Scanner(System.in);
    private static String z = "";
    public static void wew()
    {
        System.out.println("Enter name: ");
        String a = inp.nextLine();
        z = a;
    }
    public static String kuhaName()
    {
        return z;
    }
}

And then SampleRun.java should be like this: 然后SampleRun.java应该是这样的:

//import java.util.Scanner;//no need to import
public class SampleReturn
{
    public static void main(String[] args)
    {
        GetValues.wew();//First input the name .
        String nameMo = GetValues.kuhaName();//Retrieve the name
        System.out.print("Your name is: " +nameMo);//Display the name
    }
}

You have a few problems going on in this code. 您在此代码中遇到了一些问题。

First of all, you can't have a method within another method. 首先,你不能在另一个方法中有一个方法。 Second of all, you're never calling wew which will actually read the input. 其次重要的是,你永远叫wew将实际读取输入。 Assuming you meant something like this: 假设你的意思是这样的:

public class GetValues{
    Scanner inp = new Scanner(System.in);
    private static String z;

    public static void wew(){
        System.out.println("Enter name: ");
        String a = inp.nextLine();

        z = a;
    }

    public static String kuhaName(){
        return z;
    }
}

All you have to do now is call your methods in order. 您现在要做的就是按顺序调用您的方法。

Well the first thing that comes to mind is that the wew method doesn't have an ending brace } 首先想到的是wew方法没有结束括号}

you also can't declare a field inside of your method. 你也不能在你的方法中声明一个字段。 you probably want to declare private static String z outside of the wew method. 你可能想在wew方法之外声明private static String z


I've read your assignment, and althought I'm not going to entirely do your homework for you, I can give you advice on the architecture of your program. 我已经阅读了你的作业,虽然我不会完全为你完成你的作业,但我可以就你的程序架构给你建议。

First I'd make a class called Person . 首先,我要创建一个名为Person的类。 You can call your class whatever you want, but for the rest of this post, I'll refer to this class as the Person class. 您可以随意调用您的课程,但对于本文的其余部分,我将此类称为Person类。

now you'll want your class to have: 现在你希望你的班级有:

  • a field to store the name of your Person 用于存储Person name的字段
  • a method to get input from the user, and to put that into the name field. 从用户获取输入并将其放入name字段的方法。
  • a method to print the content of the name field. 打印name字段内容的方法。

in your main method you'll want to 在你想要的main方法中

  • instantiate your Person 实例化你的Person
  • call it's input-getting method 称之为输入获取方法
  • call it's printing method. 称之为打印方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM