gpt4 book ai didi

java - readInt 和 readLine 在 Java 中制作基本的加法机

转载 作者:行者123 更新时间:2023-12-02 10:01:19 24 4
gpt4 key购买 nike

我正在尝试制作一个简单的加法机。

public class AddingMachine {   // Save as 'AddingMachine.java"
public static void main(String[] args) {
System.out.println();
System.out.println("Welcome to the Adding Machine.");
System.out.println();

String name = readLine("What is your name? ");
int num1 = readInt("What is the first number? ");
int num2 = readInt("What is the second number? ");
System.out.println();

int sum = num1 + num2;
System.out.print(name);
System.out.print(", the sum is: ");
System.out.print(sum);
}
}

错误消息报告“找不到符号”readIntreadString但我在Oracle网站上找到了方法:

https://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html

可能还有其他一些方法可以解决这个问题,但我认为read方法构成了一个非常简单的输入/输出示例。

最佳答案

这就是解决方案

readInt 和 readLine 是 Scanner 类的方法。

public class AddingMachine {   // Save as 'AddingMachine.java"
public static void main(String[] args) {
System.out.println();
System.out.println("Welcome to the Adding Machine.");
System.out.println();

System.out.println("What is the first number?");
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
System.out.println("What is the second number?");
int num2 = sc.nextInt();
System.out.println();

int sum = num1 + num2;
System.out.print(sum);
System.out.print(", the sum is: ");
System.out.print(sum);
}
}

关于java - readInt 和 readLine 在 Java 中制作基本的加法机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55612298/

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