简体   繁体   中英

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. 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. Help is GREATLY appreciated.

Here is the code I worked on so far: 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. Try to input one of the numeric types for 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

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. In the above uncorrected example, earned = (35*regpay)+overh; will always be calculated, because it is not in the scope of the else statement

String can't be used the same as Integers, in Java following line doesn't compile :

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 .

As mentioned the input from user is a String, which can be converted to a float or 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. Here is an example:

String test = "r";
try{
int integer = Integer.parseInt(test);
}catch (NumberFormatException e) {
    System.out.println(e);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM