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類型和long類型之間的轉(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、把字符串類型的時(shí)間轉(zhuǎn)換為long類型
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類型的時(shí)間變成String類型
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類型轉(zhuǎn)換成string類型 String tt=topare(t2); System.out.println(tt); }//把字符串類型的時(shí)間轉(zhuǎn)換為long類型 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類型的時(shí)間變成String類型 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. 使用python tkinter開(kāi)發(fā)一個(gè)爬取B站直播彈幕工具的實(shí)現(xiàn)代碼2. ThinkPHP6使用JWT+中間件實(shí)現(xiàn)Token驗(yàn)證實(shí)例詳解3. python 實(shí)時(shí)調(diào)取攝像頭的示例代碼4. Python使用oslo.vmware管理ESXI虛擬機(jī)的示例參考5. Python如何解決secure_filename對(duì)中文不支持問(wèn)題6. Python類成員繼承重寫(xiě)的實(shí)現(xiàn)7. ASP基礎(chǔ)入門(mén)第二篇(ASP基礎(chǔ)知識(shí))8. Python 用NumPy創(chuàng)建二維數(shù)組的案例9. python安裝sklearn模塊的方法詳解10. 不使用XMLHttpRequest對(duì)象實(shí)現(xiàn)Ajax效果的方法小結(jié)
