我正在尝试将用户输入添加到我的班级中,以便用户可以输入他们的包装重量,并且我的班级将告诉他们将花多少钱。我是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?");
}
}
答案 0 :(得分:1)
简单的例子是这样的: 尝试在文档中阅读更多相关内容 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