输出用户数据以显示类

时间:2013-11-25 20:48:50

标签: java class methods

package developer;
import java.util.*;
import java.lang.Math.*;

public class Developer 
{
    static Scanner console = new Scanner(System.in);

    String workType; // This will be either an app, or game
    String name;
    int pay;
    int weekPay;
    int hrsWorked;
    double tax;

    public Developer()
    {
        name  = "Ciaran";
    }

    Developer(String appType, String coderName)
    {
        workType = appType;
        name = coderName;
    }// End developer

    Developer(String appType, int pay) // Class to choose the pay rate depending on if it is a game or app  
    {
        System.out.println("Are you coding an app or a game? ");
        appType = console.next();

        if(appType == "app")
        {
            pay = 20;
        }
        if(appType == "game")
        {
            pay = 30;
        }
        else
        {
            System.out.println("Please enter either 'app' or 'game' ");
        }   
    }// End developer

    Developer(int hrsWorked, double tax, int weekPay, int pay) // Class to choose the tax bracket which the developer is in
    {
        System.out.println("Please enter how many hours you have worked this week: ");
        hrsWorked = console.nextInt();

        weekPay = hrsWorked * pay;

        if(weekPay >= 865)
        {
            tax = 0.4;
        }
        else
        {
            tax = 0.21;
        }   


    }// End developer

    Developer(int weekPay, int tax) // Gets the pay after tax
    {
        weekPay = weekPay * tax;
    }// End developer



    public void display()
    {
        System.out.println("This display method works");
        System.out.println("User: " + name);
    }


    public static void main(String[] args) 
    {
        Developer myDev = new Developer();

        myDev.display();
    } // End main

}// End public class developer

我试图让这个程序询问用户他们的名字是什么;如果他们正在开发游戏或应用程序,并且工作小时数。有了这些信息,我想计算一下开发项目的收入,包括税收。我似乎无法使用display()方法向用户询问问题但我不知道该怎么做。我希望有人可以帮助我。

2 个答案:

答案 0 :(得分:1)

System.in将从命令行读取输入。你应该用java.util.Scanner和nextLine包装它,如下所示:

Scanner scanner = new Scanner(System.in);
String user_input = scanner.nextLine();

务必检查

scanner.hasNextLine()

继续之前或者你会收到错误。

答案 1 :(得分:0)

在您的代码中可以做的事情很少,所以让我们分解一下:

1.无需制作控制台静态类型,您可以使用:

private Scanner console = new Scanner(System.in);

2.weekPay属于int类型,但您的税是双倍的,如果您不希望将weekPay转换为整数,请将其更改为:

double weekPay;

3.接下来,您正在计算税后的weekPay,所以让我们为此引入一个变量:

double weekPayAfterTax;

4.所有这些Developer()方法都是构造函数,我认为你在这里有些困惑。当然,你可以有很多构造函数,但对我们来说,让我们只保留no-params构造函数:

public Developer() {
    name = "Ciaran";
    //you could initialise all the other variables here as well,
    //I'll leave it as an exercise for you :)
}

5.让我们创建一个方法,询问所有问题并设置各自的变量:

void setData() {
    //let's get the name
    System.out.print("What's your name: ");
    name = console.nextLine();

    System.out.print("Are you coding an app or a game? ");

    //since we want the user to enter 'app' or 'game'
    //we need to loop until we got these
    //we can do this by creating endless while loop,
    //which we will end when we have correct input
    while (true) {
        workType = console.next();

        if (workType.equals("app")) {
            pay = 20.0;

            //stop the loop
            break;
        }
        else if (workType.equals("game")) {
            pay = 30.0;

            //stop the loop
            break;
        }
        else {
            System.out.print("Please enter either 'app' or 'game': ");
            //back to top
        }
    }

    //ok, we're out the loop, let's get number of hours
    System.out.print("Please enter how many hours you have worked this week: ");
    hrsWorked = console.nextInt();

    //calculate weekPay
    weekPay = hrsWorked * pay;

    if(weekPay >= 865) {
        tax = 0.4;
    }
    else {
        tax = 0.21;
    }

    //calculate pay after tax
    weekPayAfterTax = weekPay - weekPay * tax;
}

6.让我们更新display()方法以显示所有信息:

public void display() {
    System.out.println("This display method works");
    System.out.println("User: " + name);
    System.out.println("Work type: " + workType);
    System.out.println("Pay: " + pay);
    System.out.println("Week pay: " + weekPay);
    System.out.println("Week pay after tax: " + weekPayAfterTax);
}

7.在您的main方法中,您最终可以创建Developer类的实例并获取数据:

public static void main(String[] args) {
    Developer myDev = new Developer();

    myDev.setData();
    myDev.display();
}

上面的代码可以改进(例如检查用户输入的数字是否符合预期),你的问题当然可以采用不同的方式,但这是开始。

请查看一些教程以了解基础知识,例如this onethis one。最重要的是,尝试并且不要让别人因为不理解某事而让你失望。