感谢领导的话语朴实点:JAVA高手请进
来源:百度文库 编辑:杭州交通信息网 时间:2024/05/01 07:46:53
注意:你必须到Byte, Short, Integer, Long, Float, and Double 类中去查寻如何得到这些值的范围 而不是手动地填写这些值。
2.写一个叫做CountCharacters的程序。
程序 CountCharacters 负责从文件lines.txt中读取一系列的行并基于读进的行来产生报告。输出是:
Number of characters: 205
Number of vowels: 43
Number of digits: 12
Number of upper case characters: 14
Number of continuous white space sequences: 33
注意: 文件lines.txt 已提供。
字符‘a’ ‘e’ ‘i’ ‘o’ ‘u’大小写都是元音( vowels)。一个数字是如下字符的一个: ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’。一系列的一个或多个空白(white space)字符被认为是一个空白的系列。所以一个空白 (‘ ’), 或 两个空白(‘ ‘), 以此类推都算成一个空白。
提示:
(a) 使用Character类中的方法来检查行中字符的类型
(b) 参阅ReadFromFile.java来从文件读入数据
(c) 你必须把lines.txt 和你的源代码存在一个目录下
3.在这个项目里你将设计和实现一个可以产生一系列pseudorandom integers的类Pseudorandom, 这个系列从几个方面来看都是随机的。所用原理是线性叠合的方法(linear congruence method)。线性叠合方法是从一个叫做seed的数字开始。除了 seed, 其他三个所用的数字是multiplier, increment,和 modulus. 产生假随机数的公式很简单。第一个数是:
(multiplier * seed + increment)%modulus
这个公式用了java 的% 运算符, 它可以算出从整数相除中所得的余数。每次一个新的随机数被产生后,seed 就会被改变到一个新的值。比如,我们可以用 multiplier =40, increment =3641, and modulus = 729来实现一个pseudorandom number生成器。如果我们选择 seed 为1, 那么一系列将产生的数如下:
第一个数
=(multiplier*seed + increment)%modulus
=(40*1+3641)%729
=36
并且36 成为一个新的seed。
能解决一个是一个,大家帮帮忙吧
import java.io.*;
public class TextfileScan2
{
public static void main(String[] args)throws Exception
{
int b;
int k=0,j=0,m=0,g=0,n=0;
FileInputStream in=new FileInputStream("Test.txt");
while((b=in.read())!=-1)
{
k++;
char c=(char)b;
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
{
j++;
}
if(Character.isDigit(c))
{
m++;
}
if(Character.isSpaceChar(b))
{
n++;
}
if(Character.isUpperCase(c))
{
g++;
}
}
System.out.println("Number of characters:"+k);
System.out.println("Number of vowels:"+j);
System.out.println("Number of digits:"+m);
System.out.println("Number of upper case characters:"+g);
System.out.println("Number of continuous white space sequences:"+n);
}
}
文本文件名为“Test.txt”
/*******************************************************************************
* 1
*/
class JavaLimits {
public static void main( String[ ] args ) throws Exception {
String[ ] wrapperNames = "Byte Short Integer Long Float Double".split( " " );
for ( int i = 0; i < wrapperNames.length; i++ ) {
Class c = Class.forName( "java.lang." + wrapperNames[ i ] );
System.out.println( wrapperNames[ i ].toLowerCase( ) + " 的范围:" +
"\n\t下限 = " + c.getField( "MIN_VALUE" ).get( null ) +
"\n\t上限 = " + c.getField( "MAX_VALUE" ).get( null ) );
}
}
}
/*******************************************************************************
* 2
*/
import java.io.*;
class CountCharacters {
static int chars, vowels, digits, uppers, whites;
public static void main( String[ ] args ) throws Exception {
BufferedReader br = new BufferedReader( new FileReader( "lines.txt" ) );
// 把整个文件读入字符串里好处理
String text = "";
for ( String line; ( line = br.readLine( ) ) != null; )
text += '\n' + line;
text = text.substring( 1 );
chars = text.length( );
char previousChar = 'a'; // 用任何非空白字符来初始化都行
for ( int i = 0; i < text.length( ); i++ ) {
char c = text.charAt( i );
if ( "aeiouAEIOU".indexOf( c ) >= 0 ) vowels++;
if ( Character.isDigit( c ) ) digits++;
if ( Character.isUpperCase( c ) ) uppers++;
if ( Character.isWhitespace( c ) &&
! Character.isWhitespace( previousChar ) ) whites++;
previousChar = c;
}
System.out.println(
"Number of characters: " + chars +
"\nNumber of vowels: " + vowels +
"\nNumber of digits: " + digits +
"\nNumber of upper case characters: " + uppers +
"\nNumber of continuous white space sequences: " + whites );
}
}
/*******************************************************************************
* 3
*/
class Pseudorandom {
private int seed = 1,
multiplier = 40,
increment = 3641,
modulus = 729;
public int nextInt( ) {
return seed = ( multiplier * seed + increment ) % modulus;
}
public static void main( String[ ] args ) {
Pseudorandom pr = new Pseudorandom( );
for ( int i = 0; i < 50; i++ )
System.out.println( pr.nextInt( ) );
}
}
第一个很简单
public class JavaLimits {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
System.out.println("以下分别打印出JAVA简单类型的范围!");
System.out.println("Byte:"+"从"+Byte.MIN_VALUE+"到"+Byte.MAX_VALUE);
System.out.println("Short:"+"从"+Short.MIN_VALUE+"到"+Short.MAX_VALUE);
System.out.println("Integer:"+"从"+Integer.MIN_VALUE+"到"+Integer.MAX_VALUE);
System.out.println("Long:"+"从"+Long.MIN_VALUE+"到"+Long.MAX_VALUE);
System.out.println("Float:"+"从"+Float.MIN_VALUE+"到"+Float.MAX_VALUE);
System.out.println("Double:"+"从"+Double.MIN_VALUE+"到"+Double.MAX_VALUE);
}
}