2013-02-18 4 views
-1

это кусок кода, который отображает меню для оценки:я должен сделать футбол табло, но я не могу понять, как найти общий

public void scoreBoard() //the score board to keep count; need to display 4 times 
{ 
    Scanner score = new Scanner (System.in); 
    for (int k = 1; k <= 4; k++) //repeat for the 4 quarters 
    { 
     System.out.println("T : "); 
     System.out.println(touchdown * score.nextInt()); //scores of the plays made 
     System.out.println("F : "); 
     System.out.println(fieldgoal * score.nextInt()); 
     System.out.println("E : "); 
     System.out.println (extrapnt * score.nextInt()); 
     System.out.println ("P : "); 
     System.out.println (twopntcon * score.nextInt()); 
     System.out.println("S : "); 
     System.out.println(safety * score.nextInt()); 
     System.out.println ("Q : Done with quarter " + k); 
    } 
} 

я не могу, кажется, выяснить, как использовать эти значения и рассчитать общий балл. пожалуйста помоги.

+0

Почему бы вам просто не сделать переменную, называемую total, и добавить каждый увеличивающий балл в эту переменную. Подобно total + = touchdown * score.nextInt(); –

+0

Вы не отслеживаете общий балл, создайте int для отслеживания. Также есть только 1 команда? как вы отслеживаете, какая команда забила? – orangegoat

+1

В чем проблема? Вы можете просто объявить переменную за пределами цикла for и увеличить ее внутри цикла с нужным номером. –

ответ

1

Я предполагаю, что ваш вопрос заключается в том, как повторно использовать значения сканера для вычислений и печати. Попробуйте это:

public void scoreBoard() 
{ 
    int total = 0; 
    Scanner score = new Scanner (System.in); 
    for (int k = 1; k <= 4; k++) //repeat for the 4 quarters 
    { 
     int touchdown_score = touchdown * score.nextInt(); 
     int fieldgoal_score = fieldgoal * score.nextInt(); 
     int extra_score = extrapnt * score.nextInt(); 
     int twopnt_score = twopntcon * score.nextInt(); 
     int safety_score = safety * score.nextInt(); 

     System.out.println("T : "); 
     System.out.println(touchdown_score); //scores of the plays made 
     System.out.println("F : "); 
     System.out.println(fieldgoal_score); 
     System.out.println("E : "); 
     System.out.println (extra_score); 
     System.out.println ("P : "); 
     System.out.println (twopnt_score); 
     System.out.println("S : "); 
     System.out.println(safety_score); 
     System.out.println ("Q : Done with quarter " + k); 
     total+= touchdown_score + fieldgoal_score + extra_score + twopnt_score + safety_score; 
    } 
} 
0

Чтение значения потока внутри System.out.println() как бросать его непосредственно в мусор. Сначала поставьте значение в переменную, чтобы сначала выполнить вычисления.

... 

    int total=0; 
    System.out.println("T : "); 
    int t = touchdown * score.nextInt(); 
    total += t; 
    System.out.println(t); //scores of the plays made 

    System.out.println("F : "); 
    int f = fieldgoal * score.nextInt(); 
    total += f; 
    System.out.println(f); 

    ...