1

現在、特定の国の最高スコアを決定するためのプログラムに固執しています。私が抱えている問題は、ユーザーからの入力を介して、スコアが最も高い国を取得する方法を決定することです。主な関心事は、最高のスコアを持つ国とそれが獲得したメダルを出力することです(例:USAは2ゴールド1シルバー1ブロンズを獲得します)。私はアメリカの方法atmだけに関心があります。気になる分野についてコメントを入れました。

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

4 に答える 4

0

優先キューを調べる独自のコンパレータ
を使用して、必要な最小値/最大値を取得できます

于 2013-02-22T22:01:51.293 に答える
0

このように考えてください。国のメダル数の入力が前のものより少ない場合は、なぜそれを保存し続けるのですか。現在の最高値の変数を用意し、必要な場合にのみ上書きします。

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

for(int i=0; i<noOfCountries; i++) {
    input = in.nextInt();
    if(input>currentHigh) {
        currentHigh=input;
    }
}
于 2013-02-22T22:03:40.820 に答える
0

これが宿題の場合、クラスについてもう学びましたか?これは、それらを使用するのに非常に良い時期のように見えます。

メダル数と国名ごとにフィールドを持つ国クラスを作成します。次に、その国が持っているすべてのメダルの値を返すsumメソッドをクラスに記述できます。次に、現在の国とスコアが最も高い国の合計を、@pattmortersの回答のようなもので確認します。

if/elseまた、私はあなたのステートメントを。と交換しますswitch。(メニュー選択のためにかなり作られています)。

これが宿題でない場合は、サンプルコードをいくつか提供できます。;)

于 2013-02-22T22:23:32.767 に答える
0

配列を使用して、合計値と国の名前を格納してみてください。状況に応じて、コードを使用できます。必ずしも完璧ではありませんが、機能します。また、tryブロックとcatchブロックを追加して、ユーザーが間違ったタイプのデータ、つまりcharではなくintを入力したときにプログラムがクラッシュしないようにすることもできます。

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

                }
                }
                }
于 2013-02-23T04:55:04.223 に答える