【问题标题】:Java scoring methodJava评分方法
【发布时间】:2013-02-22 21:57:16
【问题描述】:

目前卡在确定某个国家/地区的最高分的程序上。我遇到的问题是确定如何通过用户的输入来获得得分最高的国家。主要关注的是输出得分最高的国家和它赢得的奖牌(例如美国赢得2金1银1铜)。我只关心美国方法atm。我在我关心的区域添加了一些 cmets。

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");
        }
    }
}
}

【问题讨论】:

  • 如果这是家庭作业,它可能应该带有“家庭作业”标签。
  • 它说作业标签要被拿走。不知道我是否应该添加。

标签: java string loops


【解决方案1】:

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

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

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

【讨论】:

  • 我明白了,这实际上更有意义。但是对于 noOFCountries,它是用于预定数量的国家还是可以用于用户输入的任何数字?
  • @Plank_Boy:我只是把它作为一个例子。您可以通过 int noOfCountries=in.nextInt() 获取另一个用户输入来更改它
【解决方案2】:

查看Priority Queue
您可以使用自己的Comparator获得所需的最小值/最大值

【讨论】:

    【解决方案3】:

    如果这是家庭作业,你学习过课程吗?这看起来是使用它们的好时机。

    我会创建一个国家类,其中包含每个奖牌数量和国家名称的字段。然后,您可以在类中编写一个 sum 方法,该方法将返回该国家拥有的所有奖牌的值。然后检查当前国家与得分最高的国家之和,类似于@pattmorters 答案。

    另外,我会将您的 if/else 语句换成 switch。 (几乎是为菜单选择而制作的)。

    如果这不是家庭作业,我可以给你一些示例代码。 ;)

    【讨论】:

    • 恐怕我们在课堂上还没有涵盖那么多。我认为这是下周的主题。至于 pattmorter 的回答,这可能对程序有用。当其他输出为 int 时,唯一棘手的部分是将 country 作为字符串输出。
    • 好吧,country += country 可能不是您所期望的。您需要找到一种将信息映射在一起的方法。您可以为每个项目使用一个数组。使用这个你会知道如果sum[1] &gt; sum[2]country[1] 当前是赢家。那有意义吗? Java 字符串连接将处理输出混合字符串和 int 值,您不必担心。
    【解决方案4】:

    您可以尝试使用数组来存储总和值和国家/地区名称。对于您的情况,您可以使用代码。它不一定是完美的,但它确实有效。您还可以添加 try 和 catch 块,以确保在用户输入错误类型的数据(即 int 而不是 char)时程序不会崩溃。

     import java.util.Scanner;
     class OlympicMedalsEM
      {
        public static void main (String[] args)
          {
            Scanner kb = new Scanner(System.in);
            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: ");
            int num = kb.nextInt();
            int []sum = new int[num];            // Create sum array with "num"         number of elements, each representing sum for one country
            country = new String[num];           
            {
            /** each country is saved in a string array "country" element with corresponding sum in the "sum" array */
            for (int x=0; x < num; x++)
                {
                System.out.println("Enter country and medal count(from gold to bronze): ");
                country[x] = kb.next();
                goldscore = kb.nextInt()*3;
                silverscore = kb.nextInt()*2;
                bronzescore = kb.nextInt()*1;
                sum[x]= goldscore+silverscore+bronzescore;
    
                }
            }       /** gets index of array "sum" with highest value element */
                    int MaxIndex=0;
                    for (int c=0; c<sum.length-1; c++){
                       if (sum[c+1]>sum[c]){
                       MaxIndex=c+1;
                       }
                    }
                    /** country with most medals is saved in the same index number as the maximum sum index */
                    System.out.println(" the country with most medals is :" + country[MaxIndex]);
                    }
                    }
    
                    }
                    }
                    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-01
      • 2020-06-12
      • 2020-02-07
      • 2011-05-17
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2019-05-03
      • 2020-03-09
      相关资源
      最近更新 更多