###前提・実現したいこと
java
コマンドラインで、表示された、数字の列の合計を計算するプログラムを
作成しています。
###発生している問題・エラーメッセージ
正解不正解の分岐とともに、数秒たったら時間切れと表示して、
次の問題にいくようにしたいのですが、
readlineの要求が残ってしまい、何か入力しないと次の問題にいけません。
初歩的な問題で申し訳ございません。
お分かりになる方いらっしゃればお教え頂けると大変にありがいたいです。
###該当のソースコード
import java.io.*;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
public class keisan_traning {
static long sum_time = 0;
public static void main(String[] args) { long sum = 0; //long static sum_time = 0; int Correctanswer1 =0; System.out.println("陽輝三角"); // 配列の宣言。 //なんいどが高すぎるので、for文を外して一か所にする① for(int k=0;k<5;k++){ int a,b,c; a =(int)( Math.random()*1000 ) % 3+2; b =(int)( Math.random()*1000 ) % 3+2; int C[][] = new int[a][b]; for( int i=0; i<a; i++ ) { for( int j=0; j<b-i; j++ ){ c =(int)( Math.random()*1000 ) % 100+1; C[i][j] = c; sum = sum + C[i][j]; } } // 九九の結果を表示。 for( int i=0; i<a; i++ ) { for( int j=0; j<b-i; j++ ) { System.out.print(C[i][j] + " "); } System.out.println(); } //なんいどが高すぎるので、for文を外して一か所にする② //} System.out.println("合計を答えよ"); long start = System.currentTimeMillis () ; TimerTask task = new TimerTask() { public void run() { System.out.println("時間切れです"); //long time =stop-start; keisan_traning.sum_time=keisan_traning.sum_time+10000L ; } }; Timer timer = new Timer(); timer.schedule(task, 10000L); try { BufferedReader input = new BufferedReader (new InputStreamReader (System.in)); String str2 = input.readLine( ); long stop = System.currentTimeMillis () ; try{ int str3 = Integer.parseInt(str2); if(str3==sum){ System.out.println( "正解" ); System.out.println( "正解。掛かった時間は" ); long time =stop-start; System.out.println(TimeUnit.MILLISECONDS.toSeconds(time)+"秒"); //System.exit(0); Correctanswer1 ++; keisan_traning.sum_time= keisan_traning.sum_time+time; }else if(str3!=sum){ System.out.println( "不正解" ); System.out.println( "正解は" ); System.out.println(sum); System.out.println( "不正解。掛かった時間は" ); long time =stop-start; System.out.println(TimeUnit.MILLISECONDS.toSeconds(time)+"秒"); //System.exit(0); keisan_traning.sum_time= keisan_traning.sum_time+time; } }catch(NumberFormatException e){ System.out.println("数値でない値が入力されています"); long time =stop-start; keisan_traning.sum_time= keisan_traning.sum_time+time; } } catch ( IOException e) { System.out.println( "キーボードが故障しているのかもしれません" ); } } System.out.println("正解は5問中で"+Correctanswer1+"問でした"); System.out.println("合計時間は"+TimeUnit.MILLISECONDS.toSeconds(keisan_traning.sum_time)+"秒でした"); System.exit(0); }
}

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/05/16 10:15
2016/05/16 10:57