1、String转int
int res = Integer.parseInt(s);
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s;
s = sc.nextLine();
int res = Integer.parseInt(s);
System.out.printf("%d\n",res);
}
}
2、String转double
double res = Double.parseDouble(s);
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s;
s = sc.nextLine();
double res = Double.parseDouble(s);
System.out.printf("%f",res);
}
}
3、int转String
(1)s = "" + n;
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s;
int n = sc.nextInt();
s = "" + n;
System.out.println(s);
}
}
(2)用Integer的API-toString
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main
{
static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = (int)1e5 + 10;
public static void main(String[] args) throws NumberFormatException, IOException
{
Integer n = rd.nextInt();
String str = n.toString();
pw.println(str);
pw.flush();
}
}
class MyComparator implements Comparator<Integer>
{
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
}
class rd
{
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
static String nextLine() throws IOException { return reader.readLine(); }
static String next() throws IOException
{
while(!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
static int nextInt() throws IOException { return Integer.parseInt(next()); }
static double nextDouble() throws IOException { return Double.parseDouble(next()); }
static long nextLong() throws IOException { return Long.parseLong(next()); }
static BigInteger nextBigInteger() throws IOException
{
BigInteger d = new BigInteger(rd.nextLine());
return d;
}
}
class PII implements Comparable<PII>
{
long x,y;
public PII(long x ,long y)
{
this.x = x;
this.y = y;
}
public int compareTo(PII a)
{
if(this.y-a.y != 0)
return Math.toIntExact(this.y - a.y); //按x升序排序
else return Math.toIntExact(this.x - a.x); //如果x相同,按y升序排序
}
}
class Edge
{
int a,b,c;
public Edge(int a ,int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
}
4、double转String
s = "" + n;
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s;
double n = sc.nextDouble();
s = "" + n;
System.out.println(s);
}
}
5、char转String
s = "" + n;
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s;
char n = sc.nextLine().charAt(0);
s = "" + n;
System.out.println(s);
}
}
6、int转BigInteger
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.function.BiFunction;
public class Main
{
static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = (int)1e5 + 10;
public static void main(String[] args) throws NumberFormatException, IOException
{
int n = rd.nextInt();
//方法2:主打万能简洁
BigInteger res1 = BigInteger.valueOf(n);
// 方法2:主打原始
String s = "" + n;
BigInteger res2 = new BigInteger(String.valueOf(s));
pw.println(res1);
pw.println(res2);
pw.flush();
}
}
class MyComparator implements Comparator<Integer>
{
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
}
class rd
{
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
static String nextLine() throws IOException { return reader.readLine(); }
static String next() throws IOException
{
while(!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
static int nextInt() throws IOException { return Integer.parseInt(next()); }
static double nextDouble() throws IOException { return Double.parseDouble(next()); }
static long nextLong() throws IOException { return Long.parseLong(next()); }
static BigInteger nextBigInteger() throws IOException
{
BigInteger d = new BigInteger(rd.nextLine());
return d;
}
}
class PII implements Comparable<PII>
{
long x,y;
public PII(long x ,long y)
{
this.x = x;
this.y = y;
}
public int compareTo(PII a)
{
if(this.y-a.y != 0)
return Math.toIntExact(this.y - a.y); //按x升序排序
else return Math.toIntExact(this.x - a.x); //如果x相同,按y升序排序
}
}
class Edge
{
int a,b,c;
public Edge(int a ,int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
}