简体   繁体   English

变量未初始化

[英]Variable not initialized

I'm writing a program for the "University Book Store" for a class. 我正在为“大学书店”的课程编写程序。 And I feel like everything should be working, but for publishingPrice and markup, I'm getting a "Variable not initialized" error. 而且我觉得一切都应该正常工作,但是对于PublishingPrice和标记,我遇到了“未初始化变量”错误。 Yet I gave them both values. 但是我给了他们两个价值观。 What am I doing wrong? 我究竟做错了什么? I'm using Netbeans. 我正在使用Netbeans。

import java.util.Scanner;
public class ProgrammingProject {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        //Set my variables for the program
        double publisherPrice;
        double newBook;
        double usedBook;
        double usedBookDiscount;
        double rentalBook;
        double rentalBookDiscount;
        double markup;
        int booksOrdered;

        String bookName;

        System.out.println("Enter the Book Title: ");
        bookName = keyboard.next();


        System.out.println("Enter the Amount of Books Ordered: ");
        booksOrdered = keyboard.nextInt();

                //Set the conditions for markup
        if (booksOrdered < 20) {
            markup = .40;
                    }
        if (booksOrdered >= 20 && booksOrdered < 50){
            markup = .35;
                    }
        if (booksOrdered >= 50 && booksOrdered < 100){
            markup = .30;
                    }
        if (booksOrdered >=100){
            markup = .25;
        }



        //Set calculations for program to use

        usedBookDiscount = .75;
        rentalBookDiscount = .40;
        newBook = (publisherPrice + markup);
        usedBook = ((newBook * usedBookDiscount) - usedBookDiscount);
        rentalBook = ((newBook * rentalBookDiscount - rentalBookDiscount));

        System.out.println(bookName + publisherPrice + newBook + usedBook + rentalBook);



                }
}

You only assign a value to markup within if statements. 您只能在if语句中为markup分配一个值。 The compiler doesn't keep track of what's feasible, and whether you've covered all cases... so the variable still isn't definitely assigned as far as the compiler is concerned. 编译器不会跟踪可行的方法以及是否涵盖了所有情况……因此就编译器而言,仍然不确定变量的分配。

The simplest fix is to use else instead: 最简单的解决方法是使用else

if (booksOrdered < 20) {
    markup = .40;
} else if (booksOrdered < 50) {
    markup = .35;
} else if (booksOrdered < 100) {
    markup = .30;     
} else {
    markup = .25;
}

Because the final else is unconditional, the compiler will spot that every path through this code assigns a value to markup . 因为最后else是无条件的,编译器会发现通过这个代码的每个路径分配一个值markup

That's enough for markup - but you haven't specified anything that will assign a value to publisherPrice . 这足以进行markup -但您尚未指定将为publisherPrice赋值的任何内容。 Was that meant to be set via more user input? 是通过更多用户输入来设置吗? Note that it would be easier to spot all of this if you assign values to your variables at the point of declaration, rather than declaring everything at the start of the method and then assigning values. 请注意,如果在声明时为变量赋值,而不是在方法开始时声明所有内容然后赋值,则发现所有这些变量将更加容易。

For example, instead of this: 例如,代替此:

String bookName;

System.out.println("Enter the Book Title: ");
bookName = keyboard.next();

... you can have: ... 你可以有:

System.out.println("Enter the Book Title: ");
String bookName = keyboard.next();

As another side note, you shouldn't generally use double for financial amounts - consider using BigDecimal instead. 作为另一个方面说明,你一般不应使用double的资金数量-考虑使用BigDecimal代替。

必须先使用值初始化所有变量,然后才能对其进行算术运算。

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

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