Java 将用户数据输出到显示类
我试图让这个程序问用户他们的名字是什么;如果他们正在开发游戏或应用程序,以及在上面工作的小时数。有了所有这些信息,我想计算开发人员的收入(包括税)。我似乎无法使用Java 将用户数据输出到显示类,java,class,methods,Java,Class,Methods,我试图让这个程序问用户他们的名字是什么;如果他们正在开发游戏或应用程序,以及在上面工作的小时数。有了所有这些信息,我想计算开发人员的收入(包括税)。我似乎无法使用display()方法向用户提问,我不知道该怎么办。我希望有人能帮助我 System.in将从命令行读取输入。您应该用java.util.Scanner和nextLine将其包装如下: package developer; import java.util.*; import java.lang.Math.*; public clas
display()
方法向用户提问,我不知道该怎么办。我希望有人能帮助我 System.in将从命令行读取输入。您应该用java.util.Scanner和nextLine将其包装如下:
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
一定要检查
Scanner scanner = new Scanner(System.in);
String user_input = scanner.nextLine();
在继续之前,您可能会遇到错误。在您的代码中,几乎没有什么事情可以以不同的方式完成,因此让我们对其进行细分: 1.无需设置控制台静态类型,可以使用:
scanner.hasNextLine()
2.weekPay的类型为int,但您的税额为double,如果您不希望weekPay转换为整数,请将其更改为:
private Scanner console = new Scanner(System.in);
3.稍后,您将计算税后的工作日,因此让我们引入一个变量:
double weekPay;
4.所有这些Developer()方法都是构造函数,我认为您在这里有点困惑。当然,您可以有许多构造函数,但对于我们来说,让我们只保留无参数构造函数:
double weekPayAfterTax;
5.让我们创建一个方法,该方法将询问所有问题并设置相应的变量:
public Developer() {
name = "Ciaran";
//you could initialise all the other variables here as well,
//I'll leave it as an exercise for you :)
}
6.让我们更新display()方法以显示所有信息:
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;
}
7.在main方法中,您最终可以创建一个Developer类实例并获取数据:
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);
}
上面的代码可以改进(比如检查用户是否在预期的位置输入了数字),当然您的问题可以以不同的方式解决,但这是开始
请查看一些教程以了解基础知识,例如,或。最重要的是,进行实验,不要让别人因为你不理解某些东西而贬低你。让我们避免使用
==true
反模式。)我的意思是,用户在打电话之前需要查看是否有下一行,但你是对的。如果(appType==“app”),这将不起作用。您必须将字符串与equals
进行比较,即if(“app”.equals(appType))
public static void main(String[] args) {
Developer myDev = new Developer();
myDev.setData();
myDev.display();
}