[英]How to share int between different methods java
Currently doing an assignment for uni where I have to create a sales page for tickets to the olympic. 目前,我正在为uni做作业,我必须为奥运会门票创建销售页面。 It's only basic but I was wondering how I could use the two int
s student
and general
located in the purchase class in the menu class specifically for the view tickets section. 这只是基础知识,但我想知道如何使用菜单类中专门用于查看票证部分的购买类中的两个int
student
和general
。
Here is my code below 这是我的下面的代码
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);
}
}
}
Instead of declaring 'int user, general=0, student =0;' 而不是声明“国际用户,一般= 0,学生= 0;” in the function Purchase, declare them globally in the class, that way they can be accessed ANYWHERE in your code that isn't static(unless you change the identifier to static. 在购买函数中,在类中全局声明它们,这样就可以在非静态代码中以任何方式访问它们(除非您将标识符更改为静态)。
IMPORTS_HERE
class NAME{
int user = 0, general = 0, student = 0;
//CODE HERE
}
Pro-Tip: Always initialize a variable before using it, its good practice and some types require it. 专家提示:始终在使用变量之前对其进行初始化,它的优良作法和某些类型要求使用它。 Also, don't declare a variable inside of a function, its bad practice since it reserves new memory every time you call that function! 另外,不要在函数内部声明变量,这是一种不好的做法,因为每次调用该函数都会保留新的内存! :D :d
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.