gpt4 book ai didi

java - 使用扫描仪检查负值

转载 作者:行者123 更新时间:2023-12-01 18:38:21 25 4
gpt4 key购买 nike

我刚刚为我正在参加的类(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/

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