java如何将用户输入的值传递给一个string变量,用Scanner?
展开全部
(1)Scanner是方法之一,如下
import java.util.Scanner;
public class Demo{
public static void main(String[] args){
System.out.println("请输入");
Scanner s = new Scanner(System.in);
String string = s.next();
System.out.println(string);
}
}
(2)BufferedReader,如下
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Demo{
public static void main(String[] args){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入A:");
String a=br.readLine();
System.out.println(a);
}catch(Exception e){
}
}
}
(3)通过弹框提示输入
import javax.swing.JOptionPane;
public class Demo{
public static void main(String[] args){
String str = JOptionPane.showInputDialog("请输入");
System.out.println(str);
}
}
望采纳...
import java.util.Scanner;
public class Demo{
public static void main(String[] args){
System.out.println("请输入");
Scanner s = new Scanner(System.in);
String string = s.next();
System.out.println(string);
}
}
(2)BufferedReader,如下
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Demo{
public static void main(String[] args){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入A:");
String a=br.readLine();
System.out.println(a);
}catch(Exception e){
}
}
}
(3)通过弹框提示输入
import javax.swing.JOptionPane;
public class Demo{
public static void main(String[] args){
String str = JOptionPane.showInputDialog("请输入");
System.out.println(str);
}
}
望采纳...
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询