[英]How do I generate a calculated output based on input in java?
I'm trying to create a program that takes in an integer value from a user and then prints the calculated solution. 我正在尝试创建一个程序,该程序从用户获取整数值,然后打印计算出的解决方案。 More specifically take the amount of hours worked in a week multiplied by the regpay and if the hours is over 35, regpay gets multiplied by 1 and a half. 更具体地说,将一周内的工作小时数乘以佣金,如果小时数超过35小时,则将佣金乘以1.5倍。 The calculations are right, I just can't make the hours into an input form so that I can multiply it. 计算是正确的,我只是不能将小时变成输入形式,这样我就可以将它相乘。
I'm completely new to Java and have been looking everywhere for a solution. 我是Java的新手,一直在寻找解决方案。 Help is GREATLY appreciated. 非常感谢帮助。
Here is the code I worked on so far: package chapter1_prog4; 这是我到目前为止所使用的代码:package chapter1_prog4;
import java.util.Scanner;
/**
*
* @author Brian
*/
public class Chapter1_Prog4 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
String hours;
System.out.print("Enter amount of hours worked for the week:");
hours = userInput.next();
double regpay, earned, overh;
regpay = 15;
earned = 0;
overh = 0;
if (hours<=35)
earned = (hours*regpay);
else
overh = ((hours-35)*(regpay*1.5));
earned = (35*regpay)+overh;
System.out.println( "Amount earned before taxes: $" + earned);
}
}
You are trying to multiply a String
by a double
, which does not work. 您正在尝试将String
乘以double
,这不起作用。 Try to input one of the numeric types for hours
尝试输入一种数字类型hours
Change the following part 更改以下部分
String hours;
System.out.print("Enter amount of hours worked for the week:");
hours = userInput.next();
To 至
double hours;
System.out.println("Enter amount of hours worked for the week: ");
hours = userInput.nextDouble();
There is also a mistake in the if-statement if语句中也有错误
Change 更改
if (hours<=35)
earned = (hours*regpay);
else
overh = ((hours-35)*(regpay*1.5));
earned = (35*regpay)+overh;
To 至
if (hours<=35) {
earned = (hours*regpay);
} else {
overh = ((hours-35)*(regpay*1.5));
earned = (35*regpay)+overh;
}
The thing is, if you do not wrap what follows if
and else
in those curly braces, only the first statement after each of them will be seen. 问题是,如果你不在这些花括号中包含if
和else
后面的内容,那么只会看到每个花括号后面的第一个语句。 In the above uncorrected example, earned = (35*regpay)+overh;
在上面未经修正的例子中, earned = (35*regpay)+overh;
will always be calculated, because it is not in the scope of the else
statement 将始终计算,因为它不在else
语句的范围内
String can't be used the same as Integers, in Java following line doesn't compile : String不能像Integers一样使用,在Java下面的行不能编译 :
if ("string" <= 100)
Many solutions are applicable, here's one 许多解决方案都适用,这是一个
After this line : 在此之后:
hours = userInput.next();
Add this line : 添加此行:
Integer hoursInteger = Integer.valueOf(hours);
and refactor all your references from hours to hoursInteger
. 并将所有引用从几小时重构为hoursInteger
。
As mentioned the input from user is a String, which can be converted to a float or int. 如上所述,来自用户的输入是一个String,可以转换为float或int。 Here is an example: 这是一个例子:
String hours = "3.2";
int integer = Integer.parseInt(hours);
float floatingPointNumber = Float.parseFloat(hours);
However, to be on the safe side, if you do this, you should do a try-catch so that if user inputs something other than a number, your program does not crash. 但是,为了安全起见,如果你这样做,你应该做一个try-catch,这样如果用户输入的不是数字,你的程序就不会崩溃。 Here is an example: 这是一个例子:
String test = "r";
try{
int integer = Integer.parseInt(test);
}catch (NumberFormatException e) {
System.out.println(e);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.