我如何让我的代码处理给定的最小值和一个值,如果<超过最低

时间:2015-07-06 17:32:07

标签: java

我是一名Java新手,已经在这个项目上工作了大约一个月。我的课程是一个毕业计划,向学生展示完成大学学位需要多少时间和金钱。我希望我的程序能够识别出最小的CU数量<如果学生在毕业前只剩下6个左右的CU,那么我还需要它来识别我是否输入了一个字母或负数,我以某种方式设法在代码顶部拉出来。我试着使用sum == sum,它并没有给我所需的输出。我想我需要把while(循环)放在那里的某个地方。

package gradplanner13;

import java.util.ArrayList;
import java.util.Scanner;

public class GradPlanner13 {

public static void main(String[] args) {
    int sum = 0;
    Scanner input = new Scanner(System.in);
    ArrayList<Integer> array = new ArrayList<>();
    boolean loop = true;
    System.out.println("Enter the individual CUs for your remaining courses. Enter 0 when done entering your individual CUs.");

    while (loop) {
        System.out.print("Enter CUs for individual course then press enter: ");

        if (!input.hasNextInt()) {
            input.nextLine();
            System.out.println("Only positive numbers are valid inputs.  Please try again. ");
            continue;
        }

        if (!input.hasNextInt()) {
            input.nextLine();
            System.out.println("Only postive numbers are valid inputs.  Please try again.");
            continue;
        }

        int check = input.nextInt();
        input.nextLine();
        if (check == 0) {
            loop = false;
            continue;
        } else if (check < 0) {
            System.out.println("CU values must be positive.  Try again.");
            continue;
        }
        array.add(check);

    }
    for (Integer CUs : array) {
        sum += CUs;
    }

    System.out.println("Total number of CUs for all courses: " + sum);

    double rounded = 0;
    do {
        System.out.print("How many CUs you are planning to take each term: ");
        rounded = input.nextInt();

        if (sum == sum) {
            break;
        }

        if (rounded < 12 || rounded > sum) {
            System.out.println("Take each term with a minimum of 12 CUs or the CUs you have left to complete your program if less than 12 : ");
        }

    } while (rounded < 12 || rounded > sum);

    double numTermsToCompletion = Math.ceil(sum / rounded);
    System.out.println("Number of terms to completion: " + numTermsToCompletion);
    System.out.println("Tuition cost based on number of terms to completion: $" + (numTermsToCompletion * 2890));
    System.out.println("Number of months to completion: " + (numTermsToCompletion * 6));

}

}

下面的代码是我认为我遇到问题的部分,因为我需要它来识别学生可能没有剩下最小的(12)CU并且我希望它检查以确保最小值是遇到或认识到剩下的小于最小值仍然处理输入。我试图重用while(循环)因为我知道当我尝试在代码的开头输入一个字母或负数时,程序的一部分响应正确,但是当我试图把它放到它时我显然没有正确实现循环在下面的代码中,程序运行但是当它到达代码中的那一点时不会产生任何东西。它只是运行并且不会产生任何错误。总之,我希望得到一些帮助,让我的代码能够意识到学生可能没有留下最小的CU(12)并且可能需要&lt;比那毕业,但也不接受负数或字母作为输入。

do {
        System.out.print("How many CUs you are planning to take each term: ");
        rounded = input.nextInt();

        if (sum == sum) {
            break;
        }

        if (rounded < 12 || rounded > sum) {
            System.out.println("Take each term with a minimum of 12 CUs or the CUs you have left to complete your program if less than 12 : ");
        }

    } while (rounded < 12 || rounded > sum);

所以我移动了sum == sum,我更接近我需要的位置。我仍然需要做一些研究,因为我是如何得到声明,告诉我我需要至少12,但它仍然给我正确的输出。

do {
        System.out.print("How many CUs you are planning to take each term: ");
        rounded = input.nextInt();

        if (rounded < 12 || rounded > sum) {
            System.out.println("Take each term with a minimum of 12 CUs or the CUs you have left to complete your program if less than 12 : ");
        }

          if (sum == sum) {
            break;
        }

    } while (rounded < 12 || rounded > sum);

这是输出:

Total number of CUs for all courses: 8
How many CUs you are planning to take each term: 8
Take each term with a minimum of 12 CUs or the CUs you have left to complete    your program if less than 12 : 
Number of terms to completion: 1.0
Tuition cost based on number of terms to completion: $2890.0
Number of months to completion: 6.0
BUILD SUCCESSFUL (total time: 12 seconds)

确定。根据我收到的建议,我重新考虑了这个过程并重写了一些代码,它的工作效果要好得多。现在的问题是如果用户从头开始输入0,这就是输出:

Enter the individual CUs for each individual remaining course. Enter 0 when done entering your individual CUs for each course.
Enter CUs for individual course then press enter: 0
Total number of CUs for all courses: 0
Number of terms to completion: 1
Tuition cost based on number of terms to completion: $2890
Number of months to completion: 6
BUILD SUCCESSFUL (total time: 2 seconds)

如果剩下0 CU,则不应该留下任何条款。看起来我需要改变我的循环是假的,或者像我在这里做类似的事情:

if (sum >= 12) {
    do {
        System.out.print("How many CUs you are planning to take each term?       Minimum of 12 CUs required per term:  ");
        numOfCUs = input.nextInt();
    } while (numOfCUs < 12);

    numTermsToGraduation = (int) Math.ceil(sum / (double) numOfCUs);

以下是完整的新代码:

System.out.println("Enter the individual CUs for each individual remaining course. Enter 0 when done entering your individual CUs for each course.");

package gradplanner13;

import java.util.ArrayList;
import java.util.Scanner;


public class GradPlanner13 {

public static void main(String[] args) {
    int sum = 0;
    Scanner input = new Scanner(System.in);
    ArrayList<Integer> array = new ArrayList<>();
    boolean loop = true;
    // Student enters the individual credits for each course remaining in their degree program
    System.out.println("Enter the individual CUs for each individual remaining course. Enter 0 when done entering your individual CUs for each course.");

    // loop checks to make sure inputs are positive numbers
    while (loop) {
        System.out.print("Enter CUs for individual course then press enter: ");

        if (!input.hasNextInt()) {
            input.nextLine();
            System.out.println("Only positive numbers are valid inputs.  Please try again. ");
            continue;
        }

        if (!input.hasNextInt()) {
            input.nextLine();
            System.out.println("Only postive numbers are valid inputs.  Please try again.");
            continue;
        }

        int check = input.nextInt();
        input.nextLine();
        if (check == 0) {
            loop = false;
            continue;
        } else if (check < 0) {
            System.out.println("CU values must be positive.  Try again.");
            continue;
        }

        // Calculates inputs from user
        array.add(check);

    }
    for (Integer CUs : array) {
        sum += CUs;
    }

    System.out.println("Total number of CUs for all courses: " + sum);

    int numOfCUs = 0;
    int numTermsToGraduation = 0;

    if (sum >= 12) {
        do {
            System.out.print("How many CUs you are planning to take each term? Minimum of 12 CUs required per term:  ");
            numOfCUs = input.nextInt();
        } while (numOfCUs < 12);

        numTermsToGraduation = (int) Math.ceil(sum / (double) numOfCUs);

    } else {
        numOfCUs = sum;
        numTermsToGraduation = 1;
    }

    System.out.println("Number of terms to completion: " + numTermsToGraduation);
    System.out.println("Tuition cost based on number of terms to completion: $" + (numTermsToGraduation * 2890));
    System.out.println("Number of months to completion: " + (numTermsToGraduation * 6));

}

}

2 个答案:

答案 0 :(得分:0)

据我所知,您正在尝试使用“sum == sum”来告诉您输入值是负数还是字母。这是不正确的,因为sum == sum将始终返回true。

要检查数字是否为正数,您应该使用sum&gt; 0

至于检查它实际上是一个字母而不是一个数字,当你检查输入是否是一个数字(hasNextInt)时,扫描仪会处理这个。

答案 1 :(得分:0)

看起来您正在使用if (sum == sum) {...}来检查sum的值。这不是你想要做的,因为它总是等于真。你想要做的是使用try {...} catch (InputMismatchException e) {...}。在您的情况下,它将设置如下:

...
System.out.println("Enter the individual CUs for your remaining courses. Enter 0 when done entering your individual CUs.");

int exception = 1;
while (exception = 1) {
    try {
        int someNum = input.nextInt();
        ... 
    }
    catch (InputMismatchException e) {
        exception = 1;
        System.out.println("Please enter the correct data type!");
    }
}
...

在抛出InputMismatchException的第一时刻,程序将执行catch块中的代码。