使用扫描仪检查负值

时间:2014-01-01 14:55:21

标签: java

我刚刚为我正在学习的课程编写了我的第一个java程序,该程序用于根据每个课程剩余的学分给出学生毕业信息。我已经完成了所有工作,除了检查负值所需的条目。如果您有任何想法,请参阅下文并告诉我们。提前谢谢。

package txp1;

import java.util.ArrayList;

import java.util.Scanner;

public class txp1 {

    public static void main(String[] args) {

        // TODO code application logic here
        System.out.println("Welcome to the University Graduation Calculator!");

        System.out.println();

        System.out.println("This calculator will help determine how many terms "
                + "you have remaining and your tuition total based upon credits"
                + " completed per semester.");

        System.out.println();

        double tuitionpersem = 2890;

        System.out.println("We will begin by entering the number of credits for"
                + " each class remaining toward your degree.");

        double sum = 0;

        ArrayList<Double> credit = new ArrayList<>();

        {

            System.out.println("Please enter the number of credits for each individual class on a separate line and then press enter, Q to quit:");

            Scanner in = new Scanner(System.in);
            double number = 0;
            number = Integer.parseInt(in.nextLine());
            if (number <= 0);
            {
                System.out.println("The number of credits must be greater than zero!");
            }

            while (in.hasNextDouble()) {

                credit.add(in.nextDouble());

            }

            for (int i = 0; i < credit.size(); i++) {

                sum += credit.get(i);

            }

            System.out.println("Total credits remaining: " + sum);

        }

        int perterm = 0;

        System.out.println("How many credits do you plan to take per term? ");

        Scanner in = new Scanner(System.in);

        perterm = Integer.parseInt(in.nextLine());
        if (perterm <= 0);
        {
            System.out.println("The number of credits must be greater than zero!");
        }
        double totterms = sum / perterm;

        totterms = Math.ceil(totterms);

        System.out.print("Your remaining terms: ");

        System.out.println(totterms);

        double terms = (totterms);

        System.out.print("The number of months to complete at this rate is: ");

        System.out.println(6 * terms);

        double cost = terms * 2890;

        System.out.println("The cost to complete at this rate is: " + cost);

    }

}

2 个答案:

答案 0 :(得分:4)

double number = 0;
number = Integer.parseInt(in.nextLine());
if (number <= 0);

“;”在if语句的结尾处是它的结尾。您没有使用数字&lt; = 0的结果执行任何操作。我相信你的意思是:

if (number <= 0){
   //operations….
}

请注意,您创建了double类型的数字,然后为其分配一个int(从String解析)。您可以使用nextDouble方法直接获取double,如果您计划将此数字设置为Integer,则使用int而不是double和nextInt而不是解析。有关从输入解析的更多信息,请查看扫描程序documentation

答案 1 :(得分:0)

如果你在它们的末尾加上一个分号,你的if语句会终止。在那里有效地结束你的逻辑。该程序基本上检查条件,然后继续。无论多少&lt; = 0解析为。

,它都会执行您的第二个语句
 //Does not get expected results
    if (number <= 0);
    {
        //Gets executed regardless of condition
        System.out.println("The number of credits must be greater than zero!");
    }

//Gets expected results
    if (number <= 0)
    {
        //Gets executed only if the condition returns true
        System.out.println("The number of credits must be greater than zero!");
    }

编辑:由于一些有用的输入而更改。

第二次编辑:我还会考虑在代码中添加一个循环,使用户重新输入输入以获得所需的值。如果你输入一个负值,你的代码只会吐出错误信息并继续运行,这可能会让你挠头。除非你的老师没有对此进行评分,否则会忘记我刚才所说的所有内容。 = P