Java评分方法

标签 java string loops

目前正在执行一个程序来确定某个国家/地区的最高分。我遇到的问题是确定如何通过用户的输入获得得分最高的国家/地区。主要关心的是输出得分最高的国家及其赢得的奖牌(除美国外赢得 2 金 1 银 1 铜)。我只关心美国方法的自动取款机。我对我关心的领域发表了一些评论。

import java.util.Scanner;
class OlympicMedalsEM
{
public static void main (String[] args)
{
    Scanner kb = new Scanner(System.in);
    int winner=0;
    String countrywin;
    int gold=0, silver=0, bronze=0, count=0, num=0, sum=0;      //num as in the number of countries
    int goldscore=0, silverscore=0, bronzescore=0;
    String country;
    {
    System.out.println("Please choose a scoring method. a for American or c for Canadian. ");
    char again = kb.next().charAt(0);
        if(again == 'a')//american method
        {
            // American method counts the medals with the most score. ex: gold is worth 3, silver 2, and bronze 1.
            // USA win 3 gold 1 silver 0 bronze = 11 points so to speak.
            {
            System.out.println("Enter the number of countries competing: ");
            num = kb.nextInt();
            {
            for (int x=0; x < num; x++)
                {
                System.out.println("Enter country and medal count(from gold to bronze): ");
                country = kb.next();
                gold = kb.nextInt();
                silver = kb.nextInt();
                bronze = kb.nextInt();
                country += country; // not sure if for loops works better than while. Not even sure is country should be counted
                }
            }
                    {
                    goldscore = gold * 3;
                    silverscore = silver * 2;
                    bronzescore = bronze * 1;
                    sum = goldscore + silverscore + bronzescore;
                        winner = sum;
                        // determine the highest score for the winner(?)
                        if (sum < winner) 
                    winner = sum;
                    }
                {
                // country is String need to change to a int(?). 
                // need it to also figure out how to pick the country with the high quality of medals
                // Possibly substitute countrywin instead of country?
                    System.out.println("The winner is: " + country + " with " + gold + " gold medals," + silver + " silver medals, and " + bronze + " bronze medals.");
                }
            }
        }
        else if(again == 'c')//canadian method
        {
            // Canadian method which counts the total number of medals
            {
            System.out.println("Enter the number of countries competing: ");
            num = kb.nextInt();
            {
            while (count<=num)
                {
                System.out.println("Enter country and medal count(from gold to bronze): ");
                country = kb.next();
                gold = kb.nextInt();
                silver = kb.nextInt();
                bronze = kb.nextInt();
                // need to understand the american version first before proceding
                +=;
                }
            }
                    {
                    sum = gold + silver + bronze;
                        }
                {
                    System.out.println("The winner is: " + country + " with " + gold + " gold medals," + silver + " silver medals, and " + bronze + " bronze medals.");
                }
            }
        }
        else
        {
            System.err.println("invalid answer");
        }
    }
}
}

最佳答案

这样想吧。如果一个国家的奖牌数输入比之前少,为什么还要继续存储呢。为当前最高值设置一个变量,仅在需要时覆盖它们。

int currentHigh = 0;
int noOfCountries = 10;
int input;

for(int i=0; i<noOfCountries; i++) {
    input = in.nextInt();
    if(input>currentHigh) {
        currentHigh=input;
    }
}

关于Java评分方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15033866/

相关文章:

java到python的转换: x509/dsa/sha1withdsa crypto howto?

python - 在 Python 2.7 中使用加号 (+) 连接两个字符串是否合适,而不是使用大括号 ({}) 和 `format` 连接?

java - 以下样本的正确 map 格式是什么?

python - python嵌套循环中的列表理解

java - 如何从 gradle 运行 Junit TestSuites?

java - ImageIO 可以是多线程的吗?

java - spring rabbit amqp @RabbitListener 配置消费者的最小和最大数量

php - 如何正确地分解字符串

javascript - 在 javascript JSON 中添加字符串作为键

r - 如何创建循环文件夹并返回表格和绘图的 knitr 报告