当前位置: 首页 > 知识库问答 >
问题:

在Java中,如何用value for-loop匹配年份?

东郭远航
2023-03-14

我是新的编程,所以请不要评判我。我正在做一个项目,我每年加一个分数。对于输出,它应该说最高分和它在哪一年,但我不知道如何显示与疼痛匹配的年份。

预期产出:

2002年最佳成绩为10

1999年最差为4

我的输出:

2004年最佳成绩为10

这是我的代码

import java.util.Scanner;

public class Assignment5A {
    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the number of years: ");
        int nubyear = sc.nextInt();

        int years [] = new int[nubyear];

        int i;
        int j;
        int h;
        int startyear = 0;
        int large =years[0];
        int yearcountr=0;


        System.out.print("Enter the starting year: ");
        startyear = sc.nextInt();

        for (i=0; i<nubyear; i++){
             years[i] = startyear++;
            System.out.print("Enter stat for year "+years[i]+": ");
             years[i] = sc.nextInt();
             if (large < years[i]){
                large = years[i];
                years[i] = startyear;
             }
            }
        System.out.print("Best stat was "+large+" in year "+startyear );
        }

}

共有1个答案

闻慎之
2023-03-14

如果不打算检索用户输入的所有值,则不需要数组。当用户输入每个值时,您可以只对最高值进行比较。

import java.util.Scanner;

public class Assignment5A {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the number of years: ");
        int nubyear = sc.nextInt();

        System.out.print("Enter the starting year: ");
        int startyear = sc.nextInt();

        int highYear = 0; //variable to hold the year with the highest stat
        int highStat = 0; //variable to hold the current highest stat
        
        for (int i = 0; i < nubyear; i++) { 
            int tempYear = startyear++; //Hold the current year value until the comparison is done.
            System.out.print("Enter stat for year " + tempYear + ": ");
            int tempStat = sc.nextInt(); // Hold the current year stat until the comparison is done.
            if (highStat < tempStat) { //If the value entered is higher than the previous one, save the year and stat.
                highYear = tempYear;
                highStat = tempStat;
            }
        }       
        System.out.print("Best stat was " + highStat + " in year " + highYear);
    }

}
 类似资料:
  • 我试图检查字符串是否包含完全匹配。例如: String str="这是我的字符串,具有-Policy和-p" 我怎样才能做到以下几点:

  • 问题内容: 我一直在尝试使用Mockito模拟具有vararg参数的方法: 这不起作用,但是如果我改为这样做: 尽管我在对方法进行存根时已经完全省略了varargs参数,但这仍然有效。 有什么线索吗? 问题答案: Mockito 1.8.1引入了anyVararg()匹配器: 另请参阅历史记录:https : //code.google.com/archive/p/mockito/issues/6

  • 问题内容: 我已经在线阅读了文档和各种教程,但是我对regex在Java中的工作方式仍然感到困惑。我正在尝试做的是创建一个接受字符串类型参数的函数。然后,我想检查传递的字符串是否包含MDCLXVIivxlcdm以外的任何字符。因此,例如,字符串“ XMLVID​​”应返回false,而“ ABXMLVA”应返回true。 当我通过时,“ XMLIVD”,“ ABXMLVA”和“ XMLABCIX”

  • edit:作为标识字符串在模式中-请参见下面对gknicker答案的注释

  • 我在应用程序中有一个编辑文本字段,用户可以手动插入时间,我想匹配适当的日期模式,如用户可以输入这种格式的时间,只有当它不是在给定的格式,然后它显示错误。我如何才能使这个功能的模式匹配器。我不知道如何实现这个功能。有人请让我知道如何才能实现这个功能,任何帮助都将不胜感激。 谢谢

  • 这是我的作业: 编写一个模拟收银机的程序。提示用户输入三件商品的价格。将它们相加以获得小计。确定小计的税额(6%)。查找销售小计加税额的总额。显示每件商品的价格、小计金额、税额和最终金额。 我是这样做的: 然而,我需要使用循环来完成这个任务。因为提示只要求3次迭代,所以我考虑使用for循环。到目前为止,我有: 我要怎么做才能三次向用户要价?