Java 如何从数组中读取两个值并确定哪个值更大?
我是一个编程新手,有人给了我一个挑战,我被卡住了 我们有一个数组(整数[][]匹配),其中包含多个数组,每个数组都有分数(整数[]匹配结果)。如果匹配结果[0]大于匹配结果[1],则视为获胜。如果匹配项[0]小于匹配项[1],则其将被视为丢失。如果他们相等,那就是平局。我需要从比赛总数中确定胜负和平局的百分比。这是我最初得到的:Java 如何从数组中读取两个值并确定哪个值更大?,java,arrays,Java,Arrays,我是一个编程新手,有人给了我一个挑战,我被卡住了 我们有一个数组(整数[][]匹配),其中包含多个数组,每个数组都有分数(整数[]匹配结果)。如果匹配结果[0]大于匹配结果[1],则视为获胜。如果匹配项[0]小于匹配项[1],则其将被视为丢失。如果他们相等,那就是平局。我需要从比赛总数中确定胜负和平局的百分比。这是我最初得到的: public class Main { public static void main(String args[]){ Integer[][] matche
public class Main {
public static void main(String args[]){
Integer[][] matches = {{2,2},{3,2},{4,1},{1,3},{3,5},{1,1},{1,0},{0,0},{1,2},{0,2},{0,3},{3,3},{1,0}};
System.out.println(results(matches));
}
public static String results(Integer[][] matches){
Integer percentWin = 0;
Integer percentLoss = 0;
Integer percentTie = 0;
Integer amountOfMatches = 13;
for (int i = 0; i<amountOfMatches; i++){
Integer nrMatch = i;
Integer[] match_result;
}
return "Wins: " + percentWin + " Losses: " + percentLoss + " Ties: " + percentTie;
}
公共类主{
公共静态void main(字符串参数[]){
整数[][]匹配={{2,2}、{3,2}、{4,1}、{1,3}、{3,5}、{1,1}、{1,0}、{0,0}、{1,2}、{0,2}、{0,3}、{1,0};
System.out.println(结果(匹配));
}
公共静态字符串结果(整数[][]匹配){
整数百分比=0;
整数百分比损失=0;
整数百分比=0;
整数amountOfMatches=13;
对于(int i=0;imatch_result=matches[i];
?谢谢immibis!刚刚更新了代码。知道为什么percentWin、percentLoss和percentTie返回为0吗?整数数学。int percentWin=(int)(100*((双)赢/amountOfMatches))
@Joey说有100场比赛,其中70场是输的。70/100==0.0*100==0。非常感谢Elliot和immibis。我将检查所有这些。代码现在运行得很好。
public class Main {
public static void main(String args[]){
Integer[][] matches = {{2,2},{3,2},{4,1},{1,3},{3,5},{1,1},{1,0},{0,0},{1,2},{0,2},{0,3},{3,3},{1,0}};
System.out.println(results(matches));
}
public static String results(Integer[][] matches){
Integer percentWin = 0;
Integer percentLoss = 0;
Integer percentTie = 0;
Integer amountOfMatches = 13;
Integer win=0; //added by me
Integer lost=0; //added by me
Integer tie=0; //added by me
for (int i = 0; i<amountOfMatches; i++){
Integer nrMatch = i;
Integer[] match_result;
//added by from here down
match_result = matches[i];//edited thanks to user:immibis
if (match_result[0]>match_result[1])
win++;
else if (match_result[0]<match_result[1])
lost++;
else
tie++;
percentWin= (win/amountOfMatches)*100;
percentLoss= (lost/amountOfMatches)*100;
percentTie= (tie/amountOfMatches)*100;
}
//it's not returning the percentages, any idea why???
return "Wins: " + percentWin + " Losses: " + percentLoss + " Ties: " + percentTie;
}