如何在不同的java方法之间共享int

时间:2016-09-01 05:35:04

标签: java

目前正在为uni工作,我必须为奥运会的门票创建销售页面。这只是基本的,但我想知道如何使用位于菜单类中的购买类中的两个int s studentgeneral 特别是对于视图门票部分。

以下是我的代码

package assignment1;

import java.util.Scanner;

public class modulisationtest {

    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        int i, user, general, student;

        Scanner s = new Scanner(System.in);

        for(i=1; i <=60;  i = i+1 )
            System.out.print("*");
        System.out.println("");
        System.out.println("Federation University olympics 2016");
        System.out.println("");
        System.out.println("Developed by Ryan Guest, Student id 30285253 for Itech1000 ");
        System.out.println("");
        for(i=1; i <=60;  i = i+1 )
            System.out.print("*");  
        Menu();
    }


    public static void Menu() {
        int user;
        Scanner s = new Scanner(System.in);
        do
        {
            System.out.println("Ticket Purchase Menu");
            System.out.println("1.Purchase");
            System.out.println("2.View Tickets");
            System.out.println("3.Exit");
            user = s.nextInt();

            switch (user)
            { 
            case 1: System.out.println("You have selected purchase"); Purchase();
            break;

            case 2: System.out.println("ticket");
            break;

            case 3:
                System.out.println("Thanks for shopping");
                System.exit(user);

            default: System.out.println("Not Valid");   
            }

        } while (user!=3);





    }       
    public static void Purchase() 

    {
        int user, general=0, student =0;
        Scanner s = new Scanner(System.in);
        {
            do
            {
                System.out.println("Ticket Purchase Menu");
                System.out.println("1.General Admission");
                System.out.println("2.Student ticket");
                System.out.println("3.Finalise order");
                System.out.println("4.Return to Menu");
                user = s.nextInt();

                switch (user)
                { 
                case 1: 
                    System.out.println("How many General tickets do you want to purchase");
                    general = s.nextInt();

                    break;

                case 2:
                    System.out.println("How many student tickets do you want to purchase");
                    student = s.nextInt();

                    break;

                case 3:
                    System.out.println("You're Buying" + " " + general + " " + "General tickets and" + " " + student + " " + "Student Tickets");

                    break;


                case 4:
                    break;

                default: System.out.println("Not Valid");
                }


            } while (user!=3&&user!=4);




        }
    }
}

1 个答案:

答案 0 :(得分:0)

而不是声明&#39; int用户,general = 0,student = 0;&#39;在函数Purchase中,在类中全局声明它们,这样就可以在代码中随时访问它们不是静态的(除非你将标识符更改为静态。

IMPORTS_HERE
class NAME{
    int user = 0, general = 0, student = 0;
    //CODE HERE
}

Pro-Tip:在使用之前始终初始化变量,其良好实践和某些类型需要它。另外,不要在函数内部声明变量,这是不好的做法,因为每次调用该函数时它都会保留新的内存! :d