入力値をそのまま表示する単純なプログラムです。
以下の「期待する結果」のように出力したいのですが、うまくいきません。
どこがいけないのでしょうか。
ex)a,b,cを順に入力し、a,b,cが出力されるようにしたい
・現状
char1
a
char2
char3
b
a
b
・期待する結果
char1.a ←a,b,cを入力
char2.b
char3.c
a ←a,b,cがこのように出力される
b
c
・プログラム
public class Sample {
static void message(char[] str){
int i = 0;
while(i < str.length) { System.out.write(str[i]); i++; } } public static void main(String[] args) throws Exception { char[] c1 = "char1.".toCharArray(); char[] c2 = "char2.".toCharArray(); char[] c3 = "char3.".toCharArray(); message(c1); System.out.write('\n'); int a = System.in.read(); message(c2); System.out.write('\n'); int b = System.in.read(); message(c3); System.out.write('\n'); int c = System.in.read(); System.out.write(a); System.out.write('\n'); System.out.write(b); System.out.write('\n'); System.out.write(c); System.out.write('\n'); }
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。