简体   繁体   English

如何根据java中的输入生成计算输出?

[英]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. 问题是,如果你不在这些花括号中包含ifelse后面的内容,那么只会看到每个花括号后面的第一个语句。 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.

相关问题 如何从 Java 中的数组生成直方图输出? - how do I generate histogram output from an array in Java? 如何根据概率在Java中生成随机字母? - How do I generate random letters in java based on probability? 如何根据Java 8 Stream过滤器的输出计算百分比 - How do I calculate a percentage based on the output of a Java 8 Stream filter 如何使用Strings和JOptionPane输出计算所得的数字? - How to do I output Calculated numbers with Strings and JOptionPane? 如何根据计算的平均值在jpanel中绘制矩形 - How do I draw rectangles in jpanel based on calculated averages 如何为 java 中的给定输入文件生成 output 文件 - How to generate output file for given input file in java 如何将输入管道传递到Java程序中,但该输入仍显示在输出中? - How do I pipe input into a java program, but have that input still show in the output? 如何在Android / Java中根据特定的起始/结束号正确生成随机整数? - How do I properly generate a random integer based on a specific start/end number in Android/Java? 如何基于Java对象的两个或多个字段为休眠生成唯一的ID? - How do I generate a unique Id for hibernate based on two or more fields of a Java object? 如何在 Java 中对来自扫描仪的用户输入进行计时,并根据特定时间间隔将该输入放入一个类别中? - How do I time the user input from a Scanner in Java, and place that input into a category based on certain time intervals?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM