gpt4 book ai didi

java - 如何让我的代码处理给定的最小值和一个值(如果 < 给定的最小值)

转载 作者:太空宇宙 更新时间:2023-11-04 13:41:12 24 4
gpt4 key购买 nike

我是一名 Java 新手,已经从事这个程序大约一个月了。我的程序是一个毕业计划,向学生展示完成大学学位需要多少时间和金钱。我希望我的程序能够识别出,如果学生毕业前只剩下 6 个左右的 CU,那么最小的 CU 数量可能小于 12,但我也需要它来识别我是否输入了字母或负数,而我设法在代码顶部输入了字母或负数。我尝试使用 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),并且可能需要 < 才能毕业,但也不接受负数或字母作为输入。

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

}

}

最佳答案

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

要检查它是否为正数,您应该使用 sum > 0。

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

关于java - 如何让我的代码处理给定的最小值和一个值(如果 < 给定的最小值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31252111/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com