传递参数方式
来源:互联网 发布:ao史密斯壁挂炉 知乎 编辑:程序博客网 时间:2024/04/29 01:50
1、通过使用public static void main(String[] args)、public static void main(String[] args)中的String类型的参数 args,可以用来输入数据
例如,运行Test 程序时,输入相关参数
这样的结果就是 args[0] = "1",args[1] = "2",args[2] = "3"
2、在使用eclipse的IDE中,使用这个方法的过程:单击右键 --> Run As --> Run Configurations -->Arguments 中输入参数.
3、String ss=JOptionPane.showInputDialog("请输入一个数","");
然后再根据自己的需要,进行类型转换.
转换成整型:int i=Integer.parseInt(ss);
转换成单精度类型:float f=Float.parseFloat(ss);
转换成双精度类型:double d=Double.parseDouble(ss);
4、使用IO流 (比较好的方法,但有点难)
(1).直接使用已有的函数 System.int.read()(比较笨拙且不灵活的方法)
1) int n = System.in.read(); int n = System.in.read();
这样只能输入进来一个字符,并将它转换成相应的ASCII嘛,例如我在运行时,我从控制台中输入 1 ,那么n = 49
2) byte[] b = new byte[100];
System.in.read(b);
byte[] b = new byte[100];
System.in.read(b); 输入进来字节串
5、用灵活点的IO流。
使用字符串BufferedReader实现输入
1)
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader中的readLine是一个比较强函数,特别注意IO流定义时的System.in这个参数
2)使用字符串BufferedReader实现输入
import java.io.*;
public class Input{
public static void main(String args[]) throws IOException{
String str;
int a;
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一个数:");
str=buf.readLine();
a=Integer.parseInt(str);
System.out.println("输入的数为:"+a);
}
}
关键代码:
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
str=buf.readLine();
6、使用scanner这个类来输入
import java.util.Scanner;
public class sumCubic{
public static void main(String[] args){
Scanner scn=new Scanner(System.in);
int n;
System.out.println("请输入数据:");
n=scn.nextInt();
System.out.println("输入的数据是:/n"+n);
}
7、如何将String类型转换成其他类型
IO输入都是数据输入进来,并且输入进来的数据都是String类型,现在我们要解决的问题就是将它们转换成其他类型.(String类型是不能直接转换成其他类型的)
以下的方法以int型说明,其余的类型大同小异
方法一:
Integer IntNumber = new Integer(String_s);
int intNumber = IntNumber.intValue();
Integer IntNumber = new Integer(String_s);
int intNumber = IntNumber.intValue();
方法二:
int intNumber = Integer.parseInt(s);
- Java参数传递方式
- Java参数传递方式
- matlab参数传递方式
- Java参数传递方式
- java 参数传递方式
- C#参数传递方式
- 参数的传递方式
- 传递参数方式
- C++参数传递方式
- JSF参数传递方式
- 方法参数传递方式
- C++参数传递方式
- java 参数传递方式
- Java参数传递方式
- 函数参数传递方式
- Java参数传递方式
- 函数参数传递方式
- 函数参数传递方式
- 爬虫调研II:Nutch的工作流程和扩展性
- Jboss Seam3 Weld Jpa在J2SE中使用之二[配置]
- Android SDK 源码下载
- 50000内素数表
- 程序
- 传递参数方式
- 调试MySQL
- Nutch距离一个商业应用的搜索引擎还有多远收藏
- 《黑马程序员》动态代理基础知识
- iOS: Remote Control of Multimedia
- FileUpLoad上传大文件时直接蹦出页面错误 无法进断点
- Dalvik 分析之准备篇
- nutch如何才能抓取到动态的url,配置文件解析
- 使用python 驱动 lotus notes发送邮件