小编g3n*_*0st的帖子

为什么我的程序在循环中跳过提示?

我的代码应该不断循环,直到输入"stop"作为员工姓名.我遇到的一个问题是,一旦它计算了第一个员工的工作时间和费率,它将再次跳过员工姓名的提示.为什么?(代码在2个单独的类中,顺便说一句)请帮助.这是我的代码:

package payroll_program_3;
import java.util.Scanner;

public class payroll_program_3
{
    public static void main(String[] args)
    {

        Scanner input = new Scanner( System.in );

        employee_info theEmployee = new employee_info();

        String eName = "";
        double Hours = 0.0;
        double Rate = 0.0;
        while(true)
        {
            System.out.print("\nEnter Employee's Name: ");
            eName = input.nextLine();
            theEmployee.setName(eName);
            if (eName.equalsIgnoreCase("stop"))
            {
                return;
            }

            System.out.print("\nEnter Employee's Hours Worked: ");
            Hours = input.nextDouble();
            theEmployee.setHours(Hours);
            while (Hours <0)    //By using this statement, the program will not
            {                   //allow negative numbers.
                System.out.printf("Hours cannot …
Run Code Online (Sandbox Code Playgroud)

java loops skip

4
推荐指数
1
解决办法
3986
查看次数

如何覆盖子类中的方法?

我有一个库存程序,包括一个数组和一个方法来计算输入的所有库存项目的总成本.我现在必须包含一个覆盖原始的子类,以包含"一个独特的功能".我创建了一个名为ItemDetails的新文件来设置原始Item的子类.我需要包含一个独特的功能并计算库存的价值,并在此子类中计算5%的重新进货费用.我只是将一些相关的线路转移到另一个班级吗?或者我写两次代码?我不知道接下来该做什么.任何帮助都很有用.谢谢.这是我到目前为止:

package inventory3;

public class ItemDetails extends Items
{
public static void override()
    {
    private String Name;
    private double pNumber, Units, Price;

public ItemDetails()
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是它应该覆盖的Item类文件:

package inventory3;

import java.lang.Comparable;                

    public class Items implements Comparable
{
       private String Name;
       private double pNumber, Units, Price;

public Items()
    {
Name = "";
pNumber = 0.0;
Units = 0.0;
Price = 0.0;
    }

public int compareTo(Object item)
    {

  Items tmp = (Items) item;


    return this.getName().compareTo(tmp.getName());
    } 


public Items(String productName, double …
Run Code Online (Sandbox Code Playgroud)

java methods overriding class

1
推荐指数
1
解决办法
2万
查看次数

我在循环时跳过第一个输入的程序时遇到问题.请帮忙

好的,所以我不得不修改我已经工作的程序来使用2个单独的类......一个用于执行任务,一个用于存储信息.一旦它完成第一次计算并进入第二个条目,它就会跳过员工姓名.为什么?请帮忙.这是代码:

package payroll_program_3;
import java.util.Scanner;

        public class payroll_program_3
{
            public static void main(String[] args)
    {

            Scanner input = new Scanner( System.in );

            employee_info theEmployee = new employee_info();

            String eName = "";
            double Hours = 0.0;
            double Rate = 0.0;
while(true)
        {
System.out.print("\nEnter Employee's Name: ");
eName = input.nextLine();
theEmployee.setName(eName);
if (eName.equalsIgnoreCase("stop"))
                {     return;
                }

System.out.print("\nEnter Employee's Hours Worked: ");
Hours = input.nextDouble();
theEmployee.setHours(Hours);
while (Hours <0)                                                                 {                                                                                                                  System.out.printf("Hours cannot be negative\n");
                    System.out.printf("Please enter hours worked\n");
                    Hours = input.nextDouble();
                    theEmployee.setHours(Hours);
                } …
Run Code Online (Sandbox Code Playgroud)

java loops infinite

0
推荐指数
1
解决办法
186
查看次数

标签 统计

java ×3

loops ×2

class ×1

infinite ×1

methods ×1

overriding ×1

skip ×1