java long轉(zhuǎn)String +Codeforces110A案例
long轉(zhuǎn)String常用的兩種方法:
long n=scanner.nextLong();String s=Long.toString(n);//第一種方法String s=String.valueOf(n);//第二種方法
代碼實(shí)例(codeforces 110A):
import java.util.Scanner; public class Main { public static void main(String[] args) { // write your code here Scanner scanner=new Scanner(System.in); long n=scanner.nextLong(); //String s=Long.toString(n);//第一種方法 String s=String.valueOf(n);//第二種方法 int count=0; for(int i=0;i<s.length();i++){ if(s.charAt(i)==’4’||s.charAt(i)==’7’){count++; } } if(count==4||count==7){ System.out.println('YES'); }else { System.out.println('NO'); } }}
補(bǔ)充知識(shí):java string類(lèi)型和long類(lèi)型之間的轉(zhuǎn)換以及獲取當(dāng)前時(shí)間
1、獲取當(dāng)前的時(shí)間
//獲取當(dāng)前的時(shí)間 public static String get(){ Date d=new Date(); SimpleDateFormat sim=new SimpleDateFormat('yyyy-MM-dd hh:mm:ss'); String time=sim.format(d); System.out.println(time); return time; }
2、把字符串類(lèi)型的時(shí)間轉(zhuǎn)換為long類(lèi)型
public static long pare(String time){ SimpleDateFormat sim=new SimpleDateFormat('yyyy-MM-dd hh:mm:ss'); long s=0; try { s=sim.parse(time).getTime(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; }
3、把long類(lèi)型的時(shí)間變成String類(lèi)型
public static String topare(long l){ Date date = new Date(l); SimpleDateFormat sim=new SimpleDateFormat('yyyy-MM-dd hh:mm:ss'); String time=sim.format(date); return time; }
一個(gè)小栗子
獲取當(dāng)前時(shí)間和半個(gè)小時(shí)之前的時(shí)間
package cn.com.tools; import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date; public class GetTime {//獲取當(dāng)前的時(shí)間 public static String get(){ Date d=new Date(); SimpleDateFormat sim=new SimpleDateFormat('yyyy-MM-dd hh:mm:ss'); String time=sim.format(d); System.out.println(time); return time; }//獲取半個(gè)小時(shí)之前的時(shí)間 public static void main(String[] args) { String t1=get(); long t0=pare(t1); long t2=t0-1800000; //把long類(lèi)型轉(zhuǎn)換成string類(lèi)型 String tt=topare(t2); System.out.println(tt); }//把字符串類(lèi)型的時(shí)間轉(zhuǎn)換為long類(lèi)型 public static long pare(String time){ SimpleDateFormat sim=new SimpleDateFormat('yyyy-MM-dd hh:mm:ss'); long s=0; try { s=sim.parse(time).getTime(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; } //把long類(lèi)型的時(shí)間變成String類(lèi)型 public static String topare(long l){ Date date = new Date(l); SimpleDateFormat sim=new SimpleDateFormat('yyyy-MM-dd hh:mm:ss'); String time=sim.format(date); return time; }}
以上這篇java long轉(zhuǎn)String +Codeforces110A案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. XML實(shí)體注入深入理解2. XML入門(mén)的常見(jiàn)問(wèn)題(三)3. 利用CSS3新特性創(chuàng)建透明邊框三角4. 詳解CSS偽元素的妙用單標(biāo)簽之美5. WMLScript腳本程序設(shè)計(jì)第1/9頁(yè)6. 不要在HTML中濫用div7. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)8. Vue3獲取DOM節(jié)點(diǎn)的3種方式實(shí)例9. Xpath語(yǔ)法格式總結(jié)10. 前端html+css實(shí)現(xiàn)動(dòng)態(tài)生日快樂(lè)代碼
