62,614
社区成员
发帖
与我相关
我的任务
分享
// 6.设计一个程序,从文本文件中读入90个百分制的分数,按如下分段:0~29、30~49、50~59、60~69、70~79、80~89、90~
// * 100
// * 统计,并输出各分数段分数的个数以及各段分数个数占总个数的百分比。文件中的分数之间用逗号隔开如:78,90,92,85,88,87,87,73,
// * 75,81,86, 最后一个数字后面也有逗号
public static void main(String[] args) {
String s = "";
try {
InputStream in = new FileInputStream(new File("c:\\1.txt"));
int i = 0;
while ((i = in.read()) != -1) {
s += (char) i;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
char[] c = s.toCharArray();
List<Integer> l = new ArrayList<Integer>();
for (int i = 0; i < c.length; i++) {
if (c[i] == ',') {
l.add(i);
}
}
l.add(s.length());
String[] s1 = new String[l.size()];
for (int x = 0; x <= l.size(); x++) {
if (x == 0)
s1[x] = s.substring(0, l.get(x).intValue());
else if (x > 0 && x < l.size()) {
s1[x] = s.substring(l.get(x - 1).intValue() + 1, l.get(x)
.intValue());
}
}
//0~29、30~49、50~59、60~69、70~79、80~89、90~100
int u = 0,k = 0,m = 0,n = 0,f = 0,t = 0,o = 0;
for (int y = 0; y < s1.length; y++) {
int a=Integer.parseInt(s1[y]);
if(a>=0&&a<=29){
u++;
}else if(a<=49){
k++;
}else if(a<=59){
m++;
}
else if(a<=69){
n++;
}
else if(a<=79){
f++;
}
else if(a<=89){
t++;
}
else if(a<=100){
o++;
}
}
System.out.println(s1.length);
System.out.println("处于0-29的有"+u+"个,占用的百分比是"+(double)(u*100/s1.length));
System.out.println("处于30-49的有"+k+"个,占用的百分比是"+(double)(k*100/s1.length));
System.out.println("处于50-59的有"+m+"个,占用的百分比是"+(double)(m*100/s1.length));
System.out.println("处于60-69的有"+n+"个,占用的百分比是"+(double)(n*100/s1.length));
System.out.println("处于70-79的有"+f+"个,占用的百分比是"+(double)(f*100/s1.length));
System.out.println("处于80-89的有"+t+"个,占用的百分比是"+(double)(t*100/s1.length));
System.out.println("处于90-100的有"+o+"个,占用的百分比是"+(double)(o*100/s1.length));
}
* * 4.按字母表顺序和逆序每隔两个字母打印,即打印出:
*
* abcdefghijklmnopqrstuvwxyz
* *****/
public static void main(String[] args) {
String s = "abcdefghijklmnopqrstuvwxyz";
char[] c = s.toCharArray();
StringBuffer sb = new StringBuffer(s);
String d = sb.reverse().toString();
char[] c1 = d.toCharArray();
String result = resever(c) + resever(c1);
System.out.println(result);
}
public static String resever(char[] c) {
int j;
String result = "";
for (int i = 0; i < c.length; i++) {
if (i % 3 == 0) {
result += c[i];
}
}
return result;
}
public class MaoPaoSort_E8 {
public static void main(String[] args) {
try {
int mp[] = new int[args.length];
int flag = 0;
for (int i = 0; i < args.length; i++) {
mp[i] = Integer.parseInt(args[i]);
if (mp[i] <=10 | mp[i]>=99)
{
flag = flag + 1;
}
}
if (flag == 0)
{
for (int i = 0; i < mp.length - 1; i++) { //使用冒泡排序法
for (int j = 0; j < mp.length - i - 1; j++) {
int temp;
if (mp[j] < mp[j+1]) {
temp = mp[j];
mp[j] = mp[j+1];
mp[j+1] = temp;
}
}
}
System.out.print("参数从大到小顺序为:");
for (int i = 0; i < mp.length; i++) {
System.out.print( mp[i] + " ");
}
System.out.println("");
System.out.print("输入参数的最大数为:" + mp[0]);
}
else
{
System.out.println("请输入10-99之间的整数参数!");
}
}
catch (Exception e) {
System.out.println("没有传递参数或者参数类型不正确!请按照规则输入参数!程序退出...");
}
}
}
public class Random_E9 {
public static void main(String[] args){
int intRet[] = new int[2];
java.util.Random r=new java.util.Random();//使用
for (int i=0; i<10; i++)
{
int n=i+1;
for (int j=0;j<2;j++)//生成一对随机数
{
intRet[j] = r.nextInt(100);
}
//比较随机数
if (intRet[0] > intRet[1]){
System.out.println("生成的第"+n+"对随机数:"+"a="+intRet[0]+",b="+intRet[1]+",a>b" );
}
if (intRet[0] < intRet[1]){
System.out.println("生成的第"+n+"对随机数:"+"a="+intRet[0]+",b="+intRet[1]+",a<b" );
}
if (intRet[0] == intRet[1]){
System.out.println("生成的第"+n+"对随机数:"+"a="+intRet[0]+",b="+intRet[1]+",a=b" );
}
}
}
}
//参考资料:http://blog.sina.com.cn/s/blog_43eb83b90102dyoy.html