Java评分法
目前仍停留在确定某个国家的最高分数的程序上。我面临的问题是如何通过用户的输入确定得分最高的国家。主要关注的是输出得分最高的国家和它获得的奖牌(前美国获得2金1银1铜)。我只关心美国的自动取款机方法。我对我关心的领域发表了一些评论Java评分法,java,string,loops,Java,String,Loops,目前仍停留在确定某个国家的最高分数的程序上。我面临的问题是如何通过用户的输入确定得分最高的国家。主要关注的是输出得分最高的国家和它获得的奖牌(前美国获得2金1银1铜)。我只关心美国的自动取款机方法。我对我关心的领域发表了一些评论 import java.util.Scanner; class OlympicMedalsEM { public static void main (String[] args) { Scanner kb = new Scanner(System.in);
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");
}
}
}
}
import java.util.Scanner;
奥林匹克梅达尔塞姆级
{
公共静态void main(字符串[]args)
{
扫描仪kb=新扫描仪(System.in);
int=0;
字符串countrywin;
int gold=0,silver=0,brown=0,count=0,num=0,sum=0;//num表示国家数量
int goldscore=0,silverscore=0,bronzescore=0;
弦国;
{
System.out.println(“请选择一种评分方法。a代表美国人,c代表加拿大人。”);
char再次=kb.next().charAt(0);
if(再次=='a')//美国方法
{
//美国方法计算得分最高的奖牌。例如:金牌价值3枚,银牌价值2枚,铜牌价值1枚。
//美国队赢得3金1银0铜=11分。
{
System.out.println(“输入竞争国家的数量:”);
num=kb.nextInt();
{
对于(int x=0;x
您可以通过使用自己的获得所需的最小/最大值。请这样想。如果一个国家的奖牌数量输入少于上一个国家,为什么要继续存储它?请为当前最高值设置一个变量,并仅在需要时覆盖它们
int currentHigh = 0;
int noOfCountries = 10;
int input;
for(int i=0; i<noOfCountries; i++) {
input = in.nextInt();
if(input>currentHigh) {
currentHigh=input;
}
}
int currentHigh=0;
int noOfCountries=10;
int输入;
对于(int i=0;icurrentHigh){
电流高=输入;
}
}
如果这是家庭作业,那么你已经学习过课程了吗?现在似乎是使用它们的好时机
我将创建一个countries类,其中包含每个奖牌数量和国家名称的字段。然后,您可以在该类中编写一个sum方法,返回该国家拥有的所有奖牌的值。然后使用类似于@Patters answer的内容检查当前国家与得分最高的国家的总和
另外,我会将您的if/else
语句替换为开关
(非常适合菜单选择)
如果这不是家庭作业,我可以给你一些示例代码您可以尝试使用数组来存储和值和国家名称。根据您的情况,您可以使用代码。它不一定完美,但可以工作。您还可以添加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]);
}
}
}
}
}
import java.util.Scanner;
奥林匹克梅达尔塞姆级
{
公共静态void main(字符串[]args)
{
扫描仪kb=新扫描仪(System.in);
int goldscore=0,silverscore=0,bronzescore=0;
字符串[]国家;
{
System.out.println(“请选择一种评分方法。a代表美国人,c代表加拿大人。”);
char再次=kb.next().charAt(0);
if(再次=='a')//美国方法
{
//美国方法计算得分最高的奖牌。例如:金牌价值3枚,银牌价值2枚,铜牌价值1枚。
//美国队赢得3金1银0铜=11分。
{
System.out.println(“输入竞争国家的数量:”);
int num=kb.nextInt();
int[]sum=new int[num];//创建元素数为“num”的sum数组,每个元素表示一个国家的总和
country=新字符串[num];
{
/**每个国家/地区都保存在字符串数组“country”元素中,“sum”数组中有相应的sum*/
对于(int x=0;x