简体   繁体   English

在Java中将值从一个void函数传递给另一个void函数

[英]Passing value from one void function to another void function in java

I am a student of class XI am doing a school project in java. 我是XI班的学生,正在用Java做一个学校项目。 I got stuck here somewhere. 我被困在这里的某个地方。 My Code is as follow. 我的代码如下。

package school.PRo;
    import java.util.Scanner;
    class Telephone_Bill
       {
           String c_name[]=new String[10];
           String c_add[]=new String[10];
           String phno[]=new String[10];
           void accept(int n)
                {
                     Telephone_Bill ob=new Telephone_Bill();
                    Scanner in=new Scanner(System.in);
                      int i;
                     String name[]=new String[10];
                     String add[]=new String[10];
                     String ph[]=new String[10];
                     for(i=0;i<n;i++)
                        {
                            System.out.println("Index Number:>>"+i+1);
                            System.out.print("Enter The name of the customer:>>");
                            name[i]=in.nextLine();
                            c_name[i]=name[i];
                            System.out.print("Enter The address of the customer:>>");
                            add[i]=in.nextLine();
                            c_add[i]=add[i];
                            System.out.print("Enter The phone number of the customer:>>");
                            ph[i]=in.nextLine();
                            phno[i]=ph[i];
                        }
                        ob.show(n);
                    }
                    static void main(String args[])
                        {
                            int n;
                            Scanner in=new Scanner(System.in);
                            Telephone_Bill ob=new Telephone_Bill();
                            System.out.print("Enter the no. of customer:>");
                            n=in.nextInt();
                            ob.accept(n);
                        }
                    void show()
                        {
                            int i=0,p;
                            while(i<p)
                                {
                                    System.out.print(c_name[i]+" "+c_add[i]+" "+phno[i]);
                                }
                            }
                        }

What I am facing problem is that when I am passing the value from function void accept() to function void show() I cannot pass that value from one void function to another void function. 我面临的问题是,当我将值从函数void accept()传递给函数void show()时,我无法将该值从一个void函数传递给另一个void函数。 Something I am going wrong. 我出了点问题。 So, I want to know how can I pass the value from one void function to another void function. 因此,我想知道如何将值从一个void函数传递给另一个void函数。 Please help me to get rid from here. 请帮助我摆脱这里。 Thanks in advance. 提前致谢。

您需要向show()方法添加一个参数。

void show(int p) {

when you pass an argument to a function, that function declaration should have an argument which can actually accept it. 当您将参数传递给函数时,该函数声明应具有一个实际上可以接受它的参数。 You are missing it here 你在这里想念它

void show() {
}

When you change you method show to 更改时,方法显示为

void show(int arg)

now your method show can receive and argument in the variable arg 现在您的方法show可以接收变量arg中的参数了

void states that, the method show doesn't return anything. void指出,方法show不返回任何内容。 It doesn't have anything to do with what argument is passed. 它与传递的参数无关。

Read this for more info 阅读以获得更多信息

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

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