- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚为我正在参加的类(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/
我在使用 Scanner 类中的 useDelimiter 时遇到一些问题。 Scanner sc = new Scanner(System.in).useDelimiter("-"); while(
我是 Java 新手。 我目前正在做一个业余项目;制作基于文本的游戏。我意识到使用 Switch 语句对于此类游戏非常有用。 这基本上就是它的工作原理。 我问用户,你想做什么? 吃 步行 等等 那么,
我正在尝试使用扫描仪从“p.addPoint(x,y);”形式的字符串中读取代码行 我想要的正则表达式格式是: *任何内容*.addPoint(*空格或无* 或 ,*空格或无* 到目前为止我所尝试的方
我正在使用 java Scanner 尝试从名为 Inventory.txt 的文本文件中提取产品信息。 此文件包含以下格式的产品数据: “Danelectro|Bass|D56BASS-AQUA|3
我是java初学者,我正在努力让这段代码正常工作。我正在尝试读取 CSV 文件,然后使用该数据计算平均值,然后返回平均值的最高最低值和平均值的摘要。 输入如下所示: Alicia Marks,89,9
当我进入/忽略文件 reg.txt 中的最后一个新行时,我需要一些有关退出的帮助。截至目前,当它到达最后一行时,我收到一个错误,其中不包含任何内容。 public String load() {
我的程序应该提示用户输入与参加了多少次考试相关的考试分数。然而,这工作得很好,当用户输入负的考试分数时,应该让他们再次重新进入所有三项考试。我的程序会保存任何非负面的考试,因此当您重新输入三项考试时,
这个问题已经有答案了: Scanner is skipping nextLine() after using next() or nextFoo()? (25 个回答) 已关闭 9 年前。 下面给出的
我想读取用户输入,例如:11 12 13 14 15 16 Scanner sc = new Scanner(System.in); while(sc.hasNext()){
String[] invalidChars = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; Scanner sc = ne
我有一个txt文件,每行包含两个单词,例如: USA 321 France 1009 ... Germany 902 如何在二维数组中逐字读取该文件?我有: List> temps = new Arr
我的 Java 作业有问题。我收到意外异常,特别是: java.util.NoSuchElementException: No line found 我正在使用 Scanner(System.in)
我有一个很大的困惑,当我们有 Sonar 服务器时 Sonar 扫描仪有什么用?当我使用 soarqube 服务器分析一个项目时,它进行了分析并且运行良好。我仍然很困惑为什么我们也需要扫描仪。 与ec
为什么我在递归方法中遇到无限循环,而没有机会输入任何符号来打破它? class Test { int key=0; void meth(){ System.out.println
我在运行此函数时遇到错误。它使用扫描仪在某个文件中查找单词。 这里是: public static boolean VerifyExistWord(File FileToSearch, String
各位程序员大家好。 我有一些代码,spring工具套件编辑器的响应也不同,也许你们中的一些聪明人知道为什么。 File inputFile = new File(System.getProperty(
是否可以从我的网络应用程序中使用 Flash 访问通用 twain 扫描仪,保存文件并将其上传到我的应用程序中? 我已经通过谷歌进行了一些搜索,但无法找到更多细节。是否有任何预制的解决方案,付费/免费
我试图在 float 组中引入一组 float 字: protected float[] a = new float [100]; public void setCoef(){ System.
我一直在做一项充当拼字游戏词典的编程作业。该程序接受用户的输入,并根据用户从菜单中请求的内容输出包含单词列表的文件。我遇到的问题与 Scanner.nextLine() 有关。 我不太清楚为什么,但由
我试图让程序询问百分比(等级),但我希望它在用户进行第一个输入并看到输出后再次询问。我在循环中遇到问题,因为未分配变量 myMark。 import java.util.Scanner; public
我是一名优秀的程序员,十分优秀!