java - 为什么我的代码不起作用(日记代码)?

标签 java

这是日记的代码。我希望用户在日记中写一些东西,这将是第一页并被放入列表中。写完后输入下一页应该开始,他在下一页写一些东西等等。 我一遍又一遍地收到错误,但我不知道为什么。写完“输入”后出现错误。我对java和所有编程/编码都是新手(如果是相同的)。抱歉,如果我问的是一个非常愚蠢的问题:/ 感谢您的每一个建议。我感谢一切,因为我想为一年后的大学学习尽可能多的知识。

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


public class NotizbuchKlasse{
    public static void Pages(){
        System.out.println("Tag 1 : Write something in your diary.");
        System.out.println("Write enter if you are done writing.");
        ArrayList<String> List = new ArrayList<String>();
        String ListInList;
        Scanner write;
        do{
            write = new Scanner(System.in);
            ListInList = write.next();}
        while (! ListInList.equals("enter"));
        System.out.println("This is now your page. Your page is gonna be 
        created after writing something new.");
        String y = ListInList;
        List.add(y);
        write.close();
        Pages();        
    }
public static void main(String[]Args){
    Pages();
}
}

day 1 : Write something in your diary.
Write enter if you are done writing.
hello diary
enter
This is now your page. Your page is gonna be created after writing something 
new.
Exception in thread "main" day 1 : Write something in your diary.
Write enter if you are done writing.
java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at NotizbuchKlasse.Pages(NotizbuchKlasse.java:14)
    at NotizbuchKlasse.Pages(NotizbuchKlasse.java:20)
    at NotizbuchKlasse.main(NotizbuchKlasse.java:38)

最佳答案

首先,当您读取操作符号时,您需要将其作为字符串读取,因为它不是整数。 此外,在比较字符串时,您应该使用 equals 方法。 最后,在执行除法时,您应该将其中一个操作数转换为 float 以获得浮点结果(否则您将获得转换后的除法 - int -)。

也就是说,您的代码应该如下所示:

import java.util.Scanner;


public class Taschenrechner {

    public static void calculator() {
        Scanner input = new Scanner(System.in);
        // Read input numbers
        System.out.println("Give me the 2 numbers first: ");
        int x = input.nextInt();
        int y = input.nextInt();

        // Read operation
        System.out.println("Now give me the operation (+,-,*,/): ");
        String operation = input.next();

        // Perform operation
        float result = 0;
        if (operation.equals("+")) {
            result = x + y;
        } else if (operation.equals("-")) {
            result = x - y;
        } else if (operation.equals("*")) {
            result = x * y;
        } else if (operation.equals("/")) {
            // Add float casting to one of the operands to obtain a float result
            result = x / ((float) y);
        } else {
            System.err.println("ERROR: Unrecognised operation " + operation);
        }

        // Print result
        System.out.println("Result: " + result);

        // Close scanner
        input.close();
    }

    public static void main(String[] args) {
        calculator();
    }
}

请注意:

  • 由于您正在复制代码,因此我已将结果打印移至函数末尾。
  • 我为不支持的操作添加了 else 情况。
  • 使用 equals 方法比较字符串。
  • 如果您愿意,可以通过 switch-case 更改 ifif-elseelse .

关于java - 为什么我的代码不起作用(日记代码)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44966687/

相关文章:

java - GUI 框架和按钮

java - 如何分割java类文件(.java)

java - Runtime.getRuntime().gc() 在 Java 8 中继续存在,有什么用处或者有什么区别吗?

java - 用于多人游戏的 MulticastSocket

java - 如何检查 keystore 文件中的证书名称和别名?

java - 有没有支持智能参数扩展、String.format等的java库

java - 我想在我的 Java 代码中将多个映射值存储在 Redis 的单个键中

java - 获取 "maximum value entered by user"吗?

java - 从 Java API 访问 keycloak 角色/用户属性

java - 更改 JCOMBOX 时组件必须显示在屏幕上以确定其位置