アプリ版:「スタンプのみでお礼する」機能のリリースについて

入力の終了条件を「改行」にする方法を教えてください。

ScannerクラスのhasNext()メソッドを使ったのですがうまくいきません。

以下が問題のプログラムコードです。

import java.util.Scanner;

public class MainStack {
public static void main (String args[]) {

Scanner scan = new Scanner(System.in);
String c = new String();
int S[] = new int[1000];
int goukei = 0;

Stack stack = new Stack();

while (scan.hasNext()) {
if (c.equals("+")) {
int a = stack.pop(S);
int b = stack.pop(S);
stack.push(a+b, S);
} else if (c.equals("-")) {
int b = stack.pop(S);
int a = stack.pop(S);
stack.push(a-b, S);
} else if (c.equals("*")) {
int a = stack.pop(S);
int b = stack.pop(S);
stack.push(a*b, S);
} else {
stack.push(Integer.parseInt(c), S);
}
}

System.out.println(stack.pop(S));
}
}

A 回答 (1件)

1行読み込んで, その読み込んだ行に対して処理すればいいんじゃない?

    • good
    • 0
この回答へのお礼

1行を読み込む方針で解決しました!

String で読み込んで char[] に変換したところプログラムが組めました

ご回答ありがとうございました

お礼日時:2015/08/12 22:08

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!