2016-09-01 71 views
2

當前正在爲uni創建一個銷售頁面,以便爲奧林匹克門票創建一個銷售頁面。這只是基本的,但我想知道如何使用位於菜單類 中的購買類中的兩個int s studentgeneral,專門用於查看故障單部分。如何在不同方法之間共享int java

這是我的代碼而不是聲明的以下

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,學生= 0;'在功能採購,全球範圍內宣佈他們在課堂上,這樣,他們可以在任何地方在你的代碼中訪問不是靜態的(除非您更改標識靜態

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

專業提示:總是初始化變量在使用它之前,它的良好實踐和一些類型需要它。另外,不要在函數內部聲明變量,因爲它會在每次調用該函數時都保留新內存,因此它的做法很糟糕!:D

相關問題