java - 使用扫描仪检查负值

标签 java

我刚刚为我正在参加的类(class)编写了第一个 Java 程序,该程序用于根据剩余每门类(class)的学分向学生提供毕业信息。除了检查负值所需的条目外,我已经完成了所有工作。请参阅下文,如果您有任何想法请告诉我。提前致谢。

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);

    }

}

最佳答案

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

“;” if 语句的末尾就是它的结束。您没有对 number <=0 的结果执行任何操作。我相信你的意思是这样的:

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

请注意,您创建了 double 类型的 number,然后为其分配一个 int (从 String 解析)。您可以使用 nextDouble 方法直接获取 double ,如果您计划将此数字作为 Integer ,则使用 int 类型代替 double ,使用 nextInt 代替解析。有关从输入解析的更多信息,请查看 Scanner documentation .

关于java - 使用扫描仪检查负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20869885/

相关文章:

java - 使用 Jackson 解析 XML 时如何忽略特定节点

Java:自定义图标问题

java - 具有相同包名的 Android 库

java - 是否可以解决不使用更广泛的异常“System.Exception”的问题?

java - com.cloudant.client.api.ClientBuilder 出现 NoClassDefFoundError 但类存在

java - 在内存中创建一个 Zip 文件

JavaFX 拖放 : accept only some file extensions

java - 从 map 列表列表中收集特定键的值 List<String,

java - ClassCastException,尽管使用Quartz Scheduler时类,包和类加载器相同

java - 访问类方法的不同签名