如何从用户输入的年份中获取最后两位数字 [英] How to get last two digit from a year which a user inputs

查看:242
本文介绍了如何从用户输入的年份中获取最后两位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经玩了一段时间了,似乎无法确定如何完成此任务.

I Have been playing with this for while, and I can not seem to get my mind around how I can get this task accomplished.

我的任务:

我需要获取用户输入的一年的最后两位数字.

I need get the last two digits of a year that a user has entered.

示例:

用户进入2014年7月2日;我需要获得2014年的最后两位数字"14"并将其除以4.这将得到3.5;但是,我将忽略".5",而只保留3.

A user enters the July 2 , 2014; I need to get the last two digits of year 2014 which is "14" and divide it by 4. This will give me 3.5; however I will disregard the ".5" and just keep the 3.

研究:

我一直在阅读我的教科书,并且看到了一种我可能可以使用的方法,其中包括字符串生成器类.但是,我的书有一个非常简短的描述,没有显示可用于建设性地完成任务的示例.

I have been reading my Textbook, and seen one approach that I may be able to use which includes the string builder class. However my book has a very brief description and shows no example which I can constructively use to accomplish me task.

进度:

到目前为止,这是我所拥有的,基本上是我想要我的程序的模板;我只需要一些帮助就可以获取一年的最后两位数字.

This is what I have so far, it is basically a template of how I want my program; I just need some help getting the last 2 digits of a year.

public class DateCalc

{
    public static void main (String[] args)
    {
    String month;
    int day;
    int year;
            Scanner  keyboard = new Scanner(System.in); 

                // receiving input 
                System.out.print( "Please Enter The Month Of The Date : ");
                month = keyboard.nextLine();
                // receiving input 
                System.out.print( "Please Enter The Day Of The Date : ");
                day = keyboard.nextInt();
                // receiving input
                System.out.print( "Please Enter The Year OF The Date : ");
                year = keyboard.nextInt();

        switch(month) 
        {  

              // keys are numbered indexes that I need for this, please disregard

            case "January" :
            int janKey = 1;
            break;


            case "February":  
            int febKey = 4;
            break;  


            case "March":  
            int marKey = 4;
            break;


            case "April":  
            int aprKey = 0; 
            break;

            case "May":  
            int maykey = 2; 
            break;

            case "June":  
            int junKey = 5; 
            break;

            case "July":  
            int julKey = 0; 
            break;

            case "August":  
            int augKey = 3; 
            break;

            case "September":  
            int septKey = 6; 
            break;

            case "October":  
            int octKey = 1; 
            break;

            case "November":  
            int novKey = 4; 
            break;

            case "December":  
            int decKey = 4; 
            break;

            //  IN MY DEFUALT CASE " inputValidation " WILL BE EXECUTED
            default:
            JOptionPane.showMessageDialog(null," Invalid Entry Please Try Again " );

        }
    }  
}                                               

推荐答案

String date = "July 2 , 2013";
String year = ""+ date.charAt(date.length()-2) + date.charAt(date.length()-1);
System.out.println(year);

int year2 = Integer.parseInt(year);
System.out.println(year2);

这篇关于如何从用户输入的年份中获取最后两位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆