简体   繁体   English

为什么我的程序打印“ null”而不是字符串?

[英]Why is my program printing “null” instead of the string?

I'm trying to create a program that will print the temperature's for each day of the week. 我正在尝试创建一个程序,将打印一周中每一天的温度。 I don't know why my code is printing "null" instead of each day of the week. 我不知道为什么我的代码打印的是“空”而不是一周的每一天。 I've never seen this error before (I'm in a beginning programming course) and I can't find any info in the textbook. 我以前从未见过此错误(我正在学习编程入门),并且在教科书中找不到任何信息。 This is my code so far... 到目前为止,这是我的代码...

import java.util.Scanner;
public class TempsInOneWeek {

public static void main(String[] args) {
    Scanner scnr = new Scanner (System.in);
    final int NUMBER_OF_DAYS = 7;
    String[] dayOfWeek = new String[NUMBER_OF_DAYS];
    int[] temp = new int [7]; //array of 7 temperatures for 7 days of the week
    int tempForDay = 0;
    int i = 0; //loop variable

    System.out.println("Enter the temperature for each day, beginning with Sunday.");

    for (i = 0; i <= temp.length; ++i) {
        temp[i] = scnr.nextInt();
        System.out.println(dayOfWeek[i] + "/'s temperature was: " + temp[i] + " degrees.");
    }

    dayOfWeek[i] = "Sunday";    
    dayOfWeek[i] = "Monday";
    dayOfWeek[i] = "Tuesday";
    dayOfWeek[i] = "Wednesday";
    dayOfWeek[i] = "Thursday";
    dayOfWeek[i] = "Friday";
    dayOfWeek[i] = "Saturday";

    temp[i] = scnr.nextInt();
    temp[i] = scnr.nextInt();
    temp[i] = scnr.nextInt();
    temp[i] = scnr.nextInt();
    temp[i] = scnr.nextInt();
    temp[i] = scnr.nextInt();
    temp[i] = scnr.nextInt();
}

Obviously the arrays are in the wrong place but it doesn't affect the output right now so I wanted to show that I'm at least trying here. 显然,数组位于错误的位置,但是它现在不影响输出,因此我想表明我至少在这里尝试。 Really my only question is how to get the day of the week to print. 确实,我唯一的问题是如何获取星期几进行打印。 And maybe a little direction on what to do with my two arrays above. 关于上面的两个数组,可能会有一些指导。

Simply move these following assignments before for loop 只需在for循环之前移动以下分配

dayOfWeek[i] = "Sunday";    
dayOfWeek[i] = "Monday";
dayOfWeek[i] = "Tuesday";
dayOfWeek[i] = "Wednesday";
dayOfWeek[i] = "Thursday";
dayOfWeek[i] = "Friday";
dayOfWeek[i] = "Saturday";

When you attempt to retrieve the data from your array... 当您尝试从数组中检索数据时...

System.out.println(dayOfWeek[i] + "/'s temperature was: " + temp[i] + " degrees.");

...you haven't actually put anything into the array at this point. ...您目前尚未将任何内容放入数组中。

When you create your array... 创建数组时...

String[] dayOfWeek = new String[NUMBER_OF_DAYS];

This creates the array and populates each index with null 这将创建数组,并使用null填充每个索引

Remember, when you declare and initialise an array, unless you explicitly populate the array, it will be filled with default values for the array's type (String in your case - and the default value for String is null). 请记住,在声明和初始化数组时,除非您明确填充该数组,否则它将使用该数组类型的默认值填充(在您的情况下为String-且String的默认值为null)。

So, you need to move the lines of code that actually populate the array to before the point at which you attempt to retrieve the data... 所以,你需要移动实际上是在你试图检索数据点的阵列来填充的代码行...

dayOfWeek[i] = "Sunday";    
dayOfWeek[i] = "Monday";
dayOfWeek[i] = "Tuesday";
dayOfWeek[i] = "Wednesday";
dayOfWeek[i] = "Thursday";
dayOfWeek[i] = "Friday";
dayOfWeek[i] = "Saturday";

for (i = 0; i <= temp.length; ++i) {
    temp[i] = scnr.nextInt();
    System.out.println(dayOfWeek[i] + "/'s temperature was: " + temp[i] + " 
    degrees.");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM