简体   繁体   English

在 Java 代码项目中实现用户输入 - 初学者

[英]Implementing User Input in Java code project - beginner

I am new here and relatively new to Java coding.我是新来的,对 Java 编码相对较新。 I have been studying Java for a while and I started to follow some basic projects available.我一直在研究 Java 一段时间,我开始关注一些可用的基本项目。 I got this project where we design a very simple bank application.我得到了这个项目,我们设计了一个非常简单的银行应用程序。 I will paste the code below and ask my question now.我将粘贴下面的代码并现在问我的问题。 In this code we define the UserName and UserId by typing them onto the code itself.在此代码中,我们通过将 UserName 和 UserId 键入到代码本身来定义它们。 If I wanted to ask the user for his name and ID, how could I do that.如果我想向用户询问他的姓名和 ID,我该怎么做。 I tried using the Scanner function but I didn't know how to get the user answers, store it in a variable and then get the BankAccount method to use those inputs.我尝试使用扫描仪 function 但我不知道如何获取用户答案,将其存储在变量中,然后获取 BankAccount 方法以使用这些输入。

Thanks in advance for all the help.提前感谢所有帮助。

_____________________________________________________________- _______________________________________________________________________-

import java.util.Scanner;

class BankingApplication {

    public static void main(String[] args) {

        BankAccount obj1 = new BankAccount("Daniel", "DR0001");
        obj1.showMenu();


    }


}

class BankAccount
{
    int balance;
    int previousTransaction;
    String customerName;
    String customerId;

    BankAccount(String cname, String cid)
    {
        customerName = cname;
        customerId = cid;

    }

    void deposit(int amount) 
    {
        if(amount != 0)
        {
        balance = (balance + amount);
        previousTransaction =  amount;

        }
    }

    void withdraw(int amount)
    {
        if(amount != 0)
        {
        balance = balance - amount;
        previousTransaction = - amount;

        }

    }

    void getPreviousTransaction()
    {
        if(previousTransaction > 0 )
        {
            System.out.println("Deposited: " + previousTransaction);
        }
        else if (previousTransaction < 0)
        {
            System.out.println("Withdrawn: " +Math.abs(previousTransaction));
        }
        else 
        {
            System.out.println("No Transaction Occured");           
        }

    }

    void showMenu()
    {
        char option = '\0';
        Scanner scanner = new Scanner(System.in);

        System.out.println("Welcome " + customerName);
        System.out.println("Your ID is " + customerId);
        System.out.println("\n");
        System.out.println("A. Check Balance");
        System.out.println("B. Deposit");
        System.out.println("C. Withdraw");
        System.out.println("D. Previous Transaction");
        System.out.println("E. Exit");


        do 
        {
            System.out.println("================================================================");
            System.out.println("Enter an option");
            System.out.println("================================================================");
            option = scanner.next().charAt(0);
            System.out.println("\n");

            switch(option) 
            {
            case 'A':
            System.out.println("----------------------------------------------------------------");
            System.out.println("Balance= " + balance);
            System.out.println("----------------------------------------------------------------");
            break;

            case 'B':
            System.out.println("----------------------------------------------------------------");
            System.out.println("Enter an amount to deposit:");
            System.out.println("----------------------------------------------------------------");
            int amount = scanner.nextInt();
            deposit(amount);
            System.out.println("\n");
            break;

            case 'C':
            System.out.println("----------------------------------------------------------------");
            System.out.println("Enter an amount to withdraw:");
            System.out.println("----------------------------------------------------------------");
            int amount2 = scanner.nextInt();
            withdraw(amount2);
            System.out.println("\n");
            break;

            case 'D':
            System.out.println("----------------------------------------------------------------");
            getPreviousTransaction();
            System.out.println("----------------------------------------------------------------");
            System.out.println("\n");
            break;

            case 'E':
            System.out.println("****************************************************************");
            break;

            default:
                System.out.println("Invalid Option! Please enter again");
                break;



            }

        }while (option != 'E');

        System.out.println("Thanks for using our services");
    }
} 

There is a very simple solution for this, and just as you guessed, it's using the Scanner library!有一个非常简单的解决方案,正如你猜到的,它使用了 Scanner 库! So first you import the library by putting this snippet of code at the top of the class所以首先你通过将这段代码放在 class 的顶部来导入库

import java.util.Scanner;

(You can also use * instead of Scanner so that you can use any library in java.util) (您也可以使用 * 代替 Scanner 以便您可以使用 java.util 中的任何库)

Then in order to use the Input class, you use the snippet of code below;然后为了使用输入 class,您使用下面的代码片段;

Scanner input = new Scanner(System.in);

Then, if you want to ask the user for his/her name, then first you prompt them by using the print commandl然后,如果你想询问用户他/她的名字,那么首先你使用 print 命令提示他们

System.out.println("What is your name?);

Lastly, you allow them to input a String value.最后,您允许他们输入一个字符串值。

String name = input.nextLine();

For an integer it would be.nextInt(), for a double it would be.nextDouble(), for a float it would be.nextFloat() and so on and so on.对于 integer,它将是.nextInt(),对于双精度数,它将是.nextDouble(),对于浮点数,它将是.nextFloat() 等等。

Please refer to the documentation in the link below for more help.请参阅以下链接中的文档以获取更多帮助。 Hope this solved your question: :D希望这解决了你的问题::D

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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