Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-在数组中返回多个字符串_Java_Arrays_Methods - Fatal编程技术网

Java-在数组中返回多个字符串

Java-在数组中返回多个字符串,java,arrays,methods,Java,Arrays,Methods,我目前正在为学校的一项作业做一个习题集,我真的快要完成了,但是在运行我的程序时,我遇到了一个逻辑错误 习题集包括显示周平均温度、最高温度、最低温度以及一周中最热和最冷的天数(如果有多个显示) 目前我遇到的问题是,如果有多天是最热或最冷的,很可能是由于我的方法,我的程序不会显示一周中的几天。(工作日) 举例说明我想要什么: 样本1: 输入一周内每天的最高温度(从周日开始): 十一, 21 15 12 21 15 九, 本周平均气温为:14.86度 本周最高气温为:21度 本周最冷的温度是:9度 一

我目前正在为学校的一项作业做一个习题集,我真的快要完成了,但是在运行我的程序时,我遇到了一个逻辑错误

习题集包括显示周平均温度、最高温度、最低温度以及一周中最热和最冷的天数(如果有多个显示)

目前我遇到的问题是,如果有多天是最热或最冷的,很可能是由于我的方法,我的程序不会显示一周中的几天。(工作日)

举例说明我想要什么: 样本1:

输入一周内每天的最高温度(从周日开始):

十一, 21 15 12 21 15 九,

本周平均气温为:14.86度

本周最高气温为:21度

本周最冷的温度是:9度

一周中最热的日子是:周一、周四 最冷的日子是:星期六

然而,我遇到的问题是,当我尝试运行代码时,会发生这种情况(从示例1复制相同的温度):

样本2:

输入一周内每天的最高温度(从周日开始):

十一, 21 15 12 21 15 九,

本周平均气温为:14.86度

本周最高气温为:21度

本周最冷的温度是:9度

一周中最热的日子是:周一//这是我的最新一期

我认为这个问题来自于我的方法“weekDay”,很可能它不是调用一周中的两个不同的日子,而是只调用一天,它会显示x次数,这取决于有多少温度与最高或最低温度相同。如果我能得到一些指导,那就太好了,谢谢

公共类test2
{
//主要方法
公共静态void main(字符串[]args)
{
//创建一个新的扫描仪
扫描仪输入=新扫描仪(System.in);
//设置数组列表
int[]templast=新int[7];
//提示用户输入并存储输入
System.out.println(“输入一周内每天的最高温度(从周日开始):”;
for(int i=0;i数组[i])
min=数组[i];
}
返回最小值;
}
公共静态字符串工作日(int i,int[]数组)
{
int[]displayWeekDay=searchTemp(数组,i);
字符串[]工作日={“星期日”、“星期一”、“星期二”、“星期三”、“星期四”、“星期五”、“星期六”};
对于(int j=0;j0&&index[j-1]==i)
继续;
否则{
指数[j]=i;
打破
}
}
}
}
收益指数;
}
}

您的weekDay()方法基本上与searchTemp()方法执行相同的操作。如果查看weekDay(),您将看到在内部调用searchTemp()。从weekDay()方法中删除searchTemp(),可以执行以下操作:

public static String weekDay(int i)
{   int currentDay = 0;
    if((i / 7) == 0){
        currentDay = i;
    }
    else{
        currentDay = i % 7;
    }
    String[] weekDay = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    return weekDay[currentDay];
}
这假设您不关心它是哪一个月,并且这个月总是从星期天开始

您的weekDay()方法基本上与searchTemp()方法执行相同的操作。如果查看weekDay(),您将看到您正在内部调用
public static String weekDay(int i)
{   int currentDay = 0;
    if((i / 7) == 0){
        currentDay = i;
    }
    else{
        currentDay = i % 7;
    }
    String[] weekDay = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    return weekDay[currentDay];
}
public class DisplayWeekTempStat {
// Main method
public static void main(String[] args) {
    // Create a new scanner
    Scanner input = new Scanner(System.in);

    // Set array list
    int[] tempList = new int[7];

    // Prompt user for input and store input
    System.out.println("Enter the hightest temperature of each day for a week (starting on Sunday): ");
    for (int i = 0; i < tempList.length; i++)
        tempList[i] = input.nextInt();

    // Averages temperature
    double avgTemp = avgTemp(tempList);
    System.out.printf("The average temperature of the week is: %.2f degree %n", avgTemp);

    // Display hottest temperature
    int maxTemp = maxTemp(tempList);
    System.out.println("The highest temperature of the week is: " + maxTemp + " degree");

    // Display coldest temperature
    int minTemp = minTemp(tempList);
    System.out.println("The coldest temperature of the week is: " + minTemp + " degree");

    // Display hottest days of the week

    System.out.print("The hottest days of the week are: ");
    String[] tempMaxWeekDay = weekDay(maxTemp, tempList);
    for (int num = 0; num < tempMaxWeekDay.length; num++) {
        if (tempMaxWeekDay[num] != null)
            System.out.println(tempMaxWeekDay[num]);
    }

    // Display the coldest days of the week
    System.out.println("\n The coldest days of the week are: ");
    String[] tempMinWeekDay = weekDay(minTemp, tempList);
    for (int num = 0; num < tempMinWeekDay.length; num++) {
        if (tempMinWeekDay[num] != null)
            System.out.println(tempMinWeekDay[num]);
    }
}

// Average the temperature
public static double avgTemp(int[] array) {
    // Set a total temperature variable
    int tempTotal = array[0];

    // Add all temperature values
    for (int i = 1; i < array.length; i++)
        tempTotal = array[i] + tempTotal;

    // Return temperature average.
    return ((double) tempTotal / array.length);
}

// Get hottest temperature
public static int maxTemp(int[] array) {
    // Set hottest day variable
    int max = array[0];

    // Check and replace max temperature
    for (int i = 1; i < array.length; i++) {
        if (max < array[i])
            max = array[i];

    }
    return max;
}

// Get coldest temperature
public static int minTemp(int[] array) {
    // Set coldest day variable
    int min = array[0];

    // Check and replace coldtest temperature
    for (int i = 1; i < array.length; i++) {
        if (min > array[i])
            min = array[i];
    }
    return min;
}

public static String[] weekDay(int i, int[] array) {
    String[] maxWeekDays = new String[7];
    String[] weekDay = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    int k = 0;
    for (int j = 0; j < weekDay.length; j++) {
        if (array[j] == i) {
            maxWeekDays[k] = weekDay[j];
            k++;
        }
    }
    return maxWeekDays;
}