0
package michael.week.pkg5;

class Employee {
String Fname ;
        String Lname;
        String PhoneNum;
        String Address;
         void setFirst(String First){
             Fname = First ;
         }
         void setlast(String Last){
             Lname  = Last ;
         }
          void setAddress (String address){
             Address = address ;
         }
         void setPhone (String Phone){
             PhoneNum  = Phone ;
         }          
         void display (){
             System.out.println ("the Fist name is :"+ Fname + "  , the last name is :        " + Lname  + "  ,the address is : "+ Address+ "  ,the phone is : "+ PhoneNum);
         }
         }

    package michael.week.pkg5;

     import java.io.BufferedReader;
     import java.io.IOException;
      import java.io.InputStreamReader;





   public class MichaelWeek5 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
    class Stck {
    Employee stck [] = new Employee[10];
    int x ;
     void stck (){
        x= -1 ;
    }
    Employee push (Employee item){
        if (x == 9){
            System.out.println ("Stack is full");
        }else stck[++x] = item ;
        return stck[x];
    }
        Employee pop (){
        if (x <0){
            System.out.println (" Stack underflow");
           return stck [x];
        }else 
            return stck[x++];
    }
    }
    InputStreamReader inp = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(inp);
    String info2 = null ;
    Stck obj = new Stck();
    Employee obj2 = new Employee ();
    int w = -1 ;
    for (int r=1 ;r>0; ){
   System.out.println("please enter add  to add new employee");
   System.out.println("please enter pop to pop the last added employee");
   System.out.println("please enter exit to exit");
   String choice = br.readLine();
   if(choice.equals("add")){
       System.out.println(w);
       if (w >= 9){
           System.out.println("you reached the maxmum number !");
           continue  ;
       } 
       else {
           w++;
           obj.stck ();
       String info  ;
       System.out.println ("please enter Employee first name :");
       info = br.readLine();

       System.out.println ("please enter Employee last name name :");
       info = br.readLine();

       System.out.println ("please enter Employee address :");
       info = br.readLine();

       System.out.println ("please enter Employee phone number :");
       info = br.readLine();
       }
   } else if(choice.equals("pop")){
           obj.pop();
           w--;
   }else if(choice.equals("exit"))
           break ;
       else {System.out.println (choice + " is wrong choice !") ;
   }
    }

    } 

   }

こんにちは、私はJavaが初めてで、このプログラムに取り組んでいます........ データをstckにプッシュする方法を知る必要がありますか? 注: プッシュのパラメーターはタイプ employee であり、Employee には First 、 Last 、 Phone 、および address が含まれます。どのように私はそれらのそれぞれをプッシュすることができます? これが私がしたことです、そして私のインストラクターはそれを拒否しました

package week.pkg5;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Week5 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
      class Employee {


     String [] Fname = new String [10];
        String[] Lname= new String [10];
        String[] PhoneNum= new String [10];
        String[] Address = new String [10];
        int x= -1 ;
         void increment(){
           x++;

        }
         void PushFirst(String First){
             Fname[x] = First ;
         }
         void Pushlast(String Last){
             Lname [x] = Last ;
         }
         void PushPhone (String Phone){
             PhoneNum [x] = Phone ;
             System.out.println ("the Fist name is :"+ Fname [x]+ "  , the last name is : " + Lname [x] + "  ,the address is : "+ Address[x] + "  ,the phone is : "+ PhoneNum[x]);
         }
         void PushAddress (String address){
             Address [x] = address ;
         }
         void pop (){
             if (x < 0){
                 System.out.println (" No Empolyee !");
             }
             else {

        System.out.println ("the Fist name is :"+ Fname [x]+ "  , the last name is : " + Lname [x] + "  ,the address is : "+ Address[x] + "  ,the phone is : "+ PhoneNum[x]);
        x--;
        }
}
 void display (){
    if (x < 0){
        System.out.println (" No Empolyee !");
    }
    else {
        for (int q = 0 ; q <=x ; q++){
        System.out.println ((q+1)+"- "+"the First name is :"+ Fname [q]+ "  , the last name is : " + Lname [q] + "  ,the address is : "+ Address[q] + "  ,the phone is : "+ PhoneNum[q]);
        }
    }
}
 }
 InputStreamReader inp = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(inp);
    Employee obj = new Employee();
    int w = -1 ;
    for (int r=1 ;r>0; ){
   System.out.println("please enter add  to add new employee");
   System.out.println("please enter display  to display all employees list");
   System.out.println("please enter pop to pop the last added employee");
   System.out.println("please enter exit to exit");
   String choice = br.readLine();

   if(choice.equals("add")){


       System.out.println(w);
       if (w >= 9){
           System.out.println("you reached the maxmum number !");
           continue  ;
       } 
       else {
           w++;
           obj.increment();
       String info  ;
       System.out.println ("please enter Employee first name :");
       info = br.readLine();
       obj.PushFirst(info);
       System.out.println ("please enter Employee last name name :");
       info = br.readLine();
       obj.Pushlast(info);
       System.out.println ("please enter Employee address :");
       info = br.readLine();
       obj.PushAddress(info);
       System.out.println ("please enter Employee phone number :");
       info = br.readLine();
       obj.PushPhone(info);}
   }  else if(choice.equals("display")){
           obj.display();
   } else if(choice.equals("pop")){
           obj.pop();
           w--;
   }else if(choice.equals("exit"))
           break ;
       else {System.out.println (choice + " is wrong choice !") ;
   }
    }

} 
 }
4

3 に答える 3

0

従業員のスタックを作成する必要があり、その後、従業員オブジェクト全体をスタックにプッシュできます。

import java.util.Stack;
...
Employee emp = new Employee();
Stack<Employee> stack = new Stack<Employee>();
stack.push(emp);
于 2012-08-04T05:20:00.303 に答える
0

以下に示すメソッドexを呼び出してデータを保持するクラスsay(ここではstck)にオブジェクトを作成した後

class std
{

  int id;
  string name;
  public:
  void set_id(int i)
  {
      id=i;
  }
  void set_name(string n)
  {
      name = n;
   }
  void disp()
  {
     system.out.println("name"+name+" and id = "+id);
  }
 };

  main()
  {
      std s = new std();
      s.set_id(1);
      s.set_name("sunmoon");
      s.disp();
 }

ここでは、s がスタックと見なされ、オブジェクトの配列を作成して n 個の人物の詳細をプッシュできるように、1 人の人物の詳細をプッシュしたことがわかります。

于 2012-08-04T05:35:28.370 に答える
0

データをモデル化する方法にいくつかの問題があります。

問題#1(すべての最大の問題):従業員は、スタックの内部表現を持つべきではありません。現実世界のオブジェクトを考えてください。あなたの例では、各従業員には次のものが必要です。

  • 苗字
  • 住所
  • 電話番号

したがって、メンバーフィールドは次のとおりです。

 String [] Fname = new String [10];
 String[] Lname= new String [10];
 String[] PhoneNum= new String [10];
 String[] Address = new String [10];
 int x= -1 ;

所属していない。これは私たちを導きます...

問題#2:スタックをモデル化した方法では、固定数のエントリしか許可されません-あなたの場合は10)。これは、スタックが機能する方法ではありません。よくわからない場合は、スタックとは何かを読んでください。LinkedListを使用して Java でスタックをモデル化するか、Java が提供する組み込みのスタックを使用できます。独自のバージョンを作成する場合、作成する必要がある重要な操作は次のとおりです。

  • 押す
  • ポップ
  • が空です

My advice is to start by modelling the employee and pratice by pushing and popping them off of a built in Stack from the JDK until you really understand the operations before you try to create your own.

于 2012-08-04T06:59:15.213 に答える