gpt4 book ai didi

java - 如何将用户输入添加到我的 Java 程序中以便在 if 语句中使用

转载 作者:行者123 更新时间:2023-11-30 07:16:34 25 4
gpt4 key购买 nike

我正在尝试将用户输入添加到我的类中,以便用户可以输入他们的包裹的重量,我的类会告诉他们需要花费多少钱。我对java很陌生,所以我知道这并不令人惊奇,但这是我目前能做的最好的事情。提前致谢

public class ShippingCosts {

public static void main(String[] args) {
// TODO Auto-generated method stub
int weight = 0;

if (0 < weight && weight <= 1)
System.out.println("The cost to ship the package is $3.50");
if (1 < weight && weight <= 3)
System.out.println("The cost to ship the package is $5.50");
if (3 < weight && weight <= 10)
System.out.println("The cost to ship the package is $9.50");
if (10 < weight && weight <= 20)
System.out.println("The cost to ship the package is $13.50");
if (20 < weight)
System.out.println("The package is too heavy to be shipped");

System.out.println("How heavy is the package?");

}

}

最佳答案

简单的例子是这样的:尝试在文档中阅读更多相关信息 Java Scanner

// read a number from System.in:
Scanner scan = new Scanner(System.in);
String s = scan.next();
int i = scan.nextInt(); //read integers

> public String next() --->it returns the next token from the scanner.

> public String nextLine()---> it moves the scanner position to the next line and returns the value as a string.

> public byte nextByte() --->it scans the next token as a byte.

> public short nextShort() --->it scans the next token as a short value.

> public int nextInt() --->it scans the next token as an int value.

> public long nextLong() --->it scans the next token as a long value.

> public float nextFloat() --->it scans the next token as a float value.

> public double nextDouble() --->it scans the next token as a double value.

提示用户输入:

Scanner input = new Scanner(System.in);

System.out.println("Please enter your name : "); //prompt user
s = input.next(); // getting a String value

System.out.println("Please enter your age : ");
i = input.nextInt(); // getting an integer

System.out.println("Please enter your salary : ");
d = input.nextDouble(); // getting a double

关于java - 如何将用户输入添加到我的 Java 程序中以便在 if 语句中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38288101/

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