は、数や操作も、それ安全なit'sのタイプを聞かせすることができますStackクラスを定義します。 ポップする前にあなたのオブジェクトに.gettype();それはあるものの種類(文字列かもしれない)と
private boolean foundOrder = false;
private boolean twoNumbers = false;
private String firstItem,secondItem;
while(!foundOrder && !stack.empty()){
if(stack.getData().getType().equals("number")){
firstItem = stack.getData().getType(); //"write the Name on a paper"
stack.pop(); //dump the thing away
if(stack.getData().getType().equals("number")){
//you found 2 types which are the same
secondItem = stack.getData().getType();
stack.pop();
twoNumbers = true;
while(twoNumbers && !foundOrder){
if(stack.getData().getType().equals("opperation")){
foundOrder = true;
} else {
stack.pop();
}
}
} else {
//number object and you start again
}
} else {
//its an operation
}
}
//If you land here it has found your order or stack is empty!
if(foundOrder){
//we found your order
} else {
//stack is empty and your order wasn´t in the stack
}
と、この安全なこれはあなたがグーグルで短い研究後に何ができるかのヒントを与えるために、コードの唯一の模式的な一種です。あなたもこれを行うことができます:)
Class myClass{
private int number;
private String opperation,type;
public myClass(int n){
setNumber(n);
}
public myClass(String s){
setOpperation(s);
}
enter code here
private void setNumber(int n){
this.number = n;
this.type = "number";
}
private void setOpperation(String s){
this.opperation = s;
this.type = "opperation";
}
public string getType(){
return type;
}
}
この質問に対する正確な回答を受け取るには、いくつかのコードを追加してください。 – desoss
'obj.getClass()。getName()'? –
ようこそスタックオーバーフロー。あなたはすでにこれをやってみましたか? [どのくらいの研究努力が必要ですか](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users)を確認してください。スタックオーバーフローはコーディングサービスではありません。あなたはあなたの問題を研究し、投稿する前に自分でコードを書いてみることを期待しています。具体的なことに固執している場合は、[最小、完全、および検証可能な例](https://stackoverflow.com/help/mcve)と試した内容の要約を含めてお手伝いください。 – Sand