- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个脚本来记录保龄球得分以及部分使用 Java 集合框架来汇总存储的记录。
这是一项正在进行的工作,但到目前为止我所拥有的是:
package mod6;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
//import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
/**
* @author Jason
*Use the collections framework and generic types to create a bowling score tracker collection.
*Design a data structure that will be used as elements in a collection.
*Each element should contain a unique name of the bowler and a list of scores and dates of the games they have played.
Then print to the screen each:
Bowler name
Number of games
Average score of all of the games
Score of their last game
Date of their last game
*/
public class Bowling {
// Function from StackOverflow to get mean
private static double calculateAverage(List <Integer> marks) {
Integer sum = 0;
if(!marks.isEmpty()) {
for (Integer mark : marks) {
sum += mark;
}
return sum.doubleValue() / marks.size();
}
return sum;
}
public static void main(String[] args) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in), 1);
// Local variables
int swValue;
int gameUID = 1;
do{
// Display menu graphics
System.out.println("============================");
System.out.println("| BOWLING SCORE TRACKER |");
System.out.println("============================");
System.out.println("| Options: |");
System.out.println("| 1. Enter a Game |");
System.out.println("| 2. Summarize Games|");
System.out.println("| 3. Exit |");
System.out.println("============================");
swValue = Keyin.inInt(" Select option: ");
ArrayList<String> names = new ArrayList<String>(); // Use Java Collections Framework for bowling player names
ArrayList<Integer> uid = new ArrayList<Integer>(); // Use Java Collections Framework for bowling game UID
ArrayList<Integer> scores=new ArrayList<Integer>(); // Use Java Collections Framework for bowling game scores
// Switch construct
switch (swValue) {
case 1: // Accept user input to record a game
System.out.println("Let's add a game!");
// Add game ID
uid.add(gameUID);
gameUID++; // Increment the bowling game UID to prepare for additional game records
// Add names
System.out.println("");
System.out.println("Please Bowler 1's name then press ENTER");
String a = stdin.readLine(); // get a line of user input as a String player name
names.add(a); // add it to the names collection/ArrayList
System.out.println("Please Bowler 2's name then press ENTER");
String b = stdin.readLine(); // get a line of user input as a String player name
names.add(b); // add it to the names collection/ArrayList
// Add scores
System.out.println("Please Bowler 1's score then press ENTER");
Scanner in = new Scanner(System.in);
int num = in.nextInt(); // get a line of user input for player's score
scores.add(num); // add it to the names collection/ArrayList
System.out.println("Please Bowler 2's score then press ENTER");
num = in.nextInt(); // get a line of user input for player's score
scores.add(num); // add it to the names collection/ArrayList
// Print Game UID
System.out.println("Game ID:" + (gameUID - 1)); // The - 1 is because I incremented gameUID already
// Print players' names
names.stream().forEach((string) -> {
System.out.println("Player: " + string);
});
// Print players' scores
scores.stream().forEach((int1) -> {
System.out.println("Player Score: " + int1);
});
break;
case 2:
System.out.println("Allow me to summarize your game(s):");
System.out.println("The average score from all of your games is:");
double mean = calculateAverage(scores);
System.out.print(mean);
System.out.println("");
break;
case 3:
System.out.println("Exit selected");
break;
default:
System.out.println("Invalid selection");
}
} while(swValue != 3);
}
}
//**********************************************************
//**********************************************************
//Program: Keyin
//Citation: I got this from
//http://www.java2s.com/Code/Java/Development-Class/Javaprogramtodemonstratemenuselection.htm
//Topics:
//1. Using the read() method of the ImputStream class
// in the java.io package
//2. Developing a class for performing basic console
// input of character and numeric types
//**********************************************************
//**********************************************************
class Keyin {
//*******************************
// support methods
//*******************************
//Method to display the user's prompt string
public static void printPrompt(String prompt) {
System.out.print(prompt + " ");
System.out.flush();
}
//Method to make sure no data is available in the
//input stream
public static void inputFlush() {
int dummy;
int bAvail;
try {
while ((System.in.available()) != 0)
dummy = System.in.read();
} catch (java.io.IOException e) {
System.out.println("Input error");
}
}
//********************************
// data input methods for
//string, int, char, and double
//********************************
public static String inString(String prompt) {
inputFlush();
printPrompt(prompt);
return inString();
}
public static String inString() {
int aChar;
String s = "";
boolean finished = false;
while (!finished) {
try {
aChar = System.in.read();
if (aChar < 0 || (char) aChar == '\n')
finished = true;
else if ((char) aChar != '\r')
s = s + (char) aChar; // Enter into string
}
catch (java.io.IOException e) {
System.out.println("Input error");
finished = true;
}
}
return s;
}
public static int inInt(String prompt) {
while (true) {
inputFlush();
printPrompt(prompt);
try {
return Integer.valueOf(inString().trim()).intValue();
}
catch (NumberFormatException e) {
System.out.println("Invalid input. Not an integer");
}
}
}
public static char inChar(String prompt) {
int aChar = 0;
inputFlush();
printPrompt(prompt);
try {
aChar = System.in.read();
}
catch (java.io.IOException e) {
System.out.println("Input error");
}
inputFlush();
return (char) aChar;
}
public static double inDouble(String prompt) {
while (true) {
inputFlush();
printPrompt(prompt);
try {
return Double.valueOf(inString().trim()).doubleValue();
}
catch (NumberFormatException e) {
System.out
.println("Invalid input. Not a floating point number");
}
}
}
}
结果是:
============================
| BOWLING SCORE TRACKER |
============================
| Options: |
| 1. Enter a Game |
| 2. Summarize Games|
| 3. Exit |
============================
Select option: 1
Let's add a game!
Please Bowler 1's name then press ENTER
Marky
Please Bowler 2's name then press ENTER
Mark
Please Bowler 1's score then press ENTER
10
Please Bowler 2's score then press ENTER
20
Game ID:1
Player: Marky
Player: Mark
Player Score: 10
Player Score: 20
============================
| BOWLING SCORE TRACKER |
============================
| Options: |
| 1. Enter a Game |
| 2. Summarize Games|
| 3. Exit |
============================
Select option: 2
Allow me to summarize your game(s):
The average score from all of your games is:
0.0
============================
| BOWLING SCORE TRACKER |
============================
| Options: |
| 1. Enter a Game |
| 2. Summarize Games|
| 3. Exit |
============================
Select option: 3
Exit selected
而我想要的结果是:
============================
| BOWLING SCORE TRACKER |
============================
| Options: |
| 1. Enter a Game |
| 2. Summarize Games|
| 3. Exit |
============================
Select option: 1
Let's add a game!
Please Bowler 1's name then press ENTER
Marky
Please Bowler 2's name then press ENTER
Mark
Please Bowler 1's score then press ENTER
10
Please Bowler 2's score then press ENTER
20
Game ID:1
Player: Marky
Player: Mark
Player Score: 10
Player Score: 20
============================
| BOWLING SCORE TRACKER |
============================
| Options: |
| 1. Enter a Game |
| 2. Summarize Games|
| 3. Exit |
============================
Select option: 2
Allow me to summarize your game(s):
The average score from all of your games is:
**15.0**
============================
| BOWLING SCORE TRACKER |
============================
| Options: |
| 1. Enter a Game |
| 2. Summarize Games|
| 3. Exit |
============================
Select option: 3
Exit selected
最佳答案
你必须把这一行
ArrayList<Integer> scores=new ArrayList<Integer>();
在 do while 循环之外
ArrayList<Integer> scores=new ArrayList<Integer>();
do{
// Switch and so on
}while (test);
因为您在每个循环中重新初始化它并删除以前的值
关于java - 如何从 Do While 封闭开关中的 Integer ArrayList 返回平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32929078/
我正在尝试执行 JavaPairRDD 和 JavaPairRDD 的 leftOuterJoin> 并且函数签名返回类型是 JavaPairRDD>>> 这里可选的是 com.google.comm
我正在尝试按元素的频率对元素进行排序 import java.io.BufferedReader; import java.io.IOException; import java.io.InputSt
这个问题已经有答案了: Is List a subclass of List? Why are Java generics not implicitly polymorphic? (19 个回答) 已
编辑:问题已解决:请参阅 Karim SNOUSSI 的答案和我在下面的评论。 这是我在堆栈溢出时遇到的第一个问题,所以我可能不会一开始就把所有事情都做对。对此感到抱歉。此外,我对 Java 和一般
#include #include using namespace std; class Integer { public: int i; Integer (int ll
我不明白: ArrayList list = new ArrayList(); Collection list1 = new ArrayList(); 类 ArrayList扩展实现接口(interf
我编写了:。它成功了。我不知道为什么?
我编写了:。它成功了。我不知道为什么
我编写了:。它成功了。我不知道为什么?
Collectors.counting()返回 long此方法中每个键的值: private static Map countDuplicates(HashSet cards) { retur
我正在尝试通过搜索旧元素并将其替换为新元素来更新节点的元素。但是有一个我不明白的错误。是什么导致我的代码出现该错误,我该如何解决?错误; The method update(Integer, Inte
我有一个称为 client 的表,其中有一列称为created_time ,所以实际上我想绘制一个 map ,以便我可以知道在哪一年和哪一个月添加了多少客户?现在的要求是假设在 2018 年 11 月
这个问题已经有答案了: Is Java "pass-by-reference" or "pass-by-value"? (91 个回答) 已关闭 8 年前。 我对 ArrayList Collecti
我意识到下面的代码是正确的 Integer.MIN_VALUE == -Integer.MIN_VALUE == Math.abs(Integer.MIN_VALUE) 这是因为当我们取反-21474
我有以下类 AccountWebappGridRow,它扩展了 AccountGridRow: public class AccountWebappGridRow extends AccountGri
我正在学习 Haskell 并看到了函数组合。 尝试复合 map和 foldl mapd = (map.foldl) 比 test = (mapd (\x y -> x + y ) [1,2,3,4]
我有两个相同大小的数组和两个方法。 public class Client { private static int[] ints; private static final int
我喜欢 Java 8 中的 Streams 概念。现在我想借助 Java Streams 将 Java 中的 Map 转换为排序列表。我只想显示列表而不将其存储在任何地方。我希望在结果列表中有这个输出
我有一个数据库表,其中包含电视节目类型列表和关联的 ARGB 颜色值,用于在显示电视指南时突出显示 Android ListView 中的电视节目。流派表看起来像这样... id genre
我有一个 Integer 类,它应该模拟一个整数 mod n。因此,它具有如下构造函数: Integer::Integer(int x) : m(x), n(0) { } Integer::I
我是一名优秀的程序员,十分优秀!