转化为百分数
NumberFormat nu = NumberFormat.getPercentInstance();
nu.setMaximumFractionDigits(0);//设置保留几位小数
System.out.println(nu.format(pass));
System.out.println(nu.format(good));
题目示例
import java.text.NumberFormat;
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double n = 0;
double avg1 = 0, avg2= 0;
n = scan.nextInt();
double[]a = new double[(int) n];
for (int i = 0; i < n; i++) {
a[i] = scan.nextInt();
if(a[i] >= 60){
avg1 ++;
}
if(a[i] >= 85){
avg2 ++;
}
}
//优秀率和及格率
double pass = 0;
double good = 0;
good = avg2/n;
pass = avg1/n;
NumberFormat nu = NumberFormat.getPercentInstance();
nu.setMaximumFractionDigits(0);//设置保留几位小数
System.out.println(nu.format(pass));
System.out.println(nu.format(good));
scan.close();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/302e8c9b15eecb7ce1321f4cec9bfa7b.png)
其实不调其他库也是可以的,但是感觉还是不适合自己
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n , x;
int cnt1 = 0 , cnt2 = 0;
n = cin.nextInt();
for (int i = 1; i <= n; i++) {
x = cin.nextInt();
if (x >= 60) {
cnt1++;
}
if (x >= 85) {
cnt2++;
}
}
System.out.printf("%.0f%%\n", (1.0 * cnt1 / n * 100));
System.out.printf("%.0f%%\n", (1.0 * cnt2 / n * 100));
cin.close();
}
}
也是一种比较不错的方法
倒序输出字符串
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
//倒序输出
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
char[] chars = str.toCharArray();
for(int i = (chars.length-1);i>=0;i--){
System.out.print(chars[i]);
scan.close();
}}}
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
String S = "WHERETHEREISAWILLTHEREISAWAY";
char[] ts = S.toCharArray();
Arrays.sort(ts);
System.out.println(ts);
}
}
关于排序的问题,真的使用sort,是比较好的,它就是按照字母顺序前后进行排序的。
字符串与数字之间的关系
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
char[] chars = str.toCharArray();
int num = Integer.parseInt(String.valueOf(chars[1]));
int a = 1189,b = 841,t = 0;
for (int i = 0; i < num; i++) {
a = a/2;
if(a<b){
t = b;
b = a;
a = t;
}
}
System.out.println(a);
System.out.println(b);
}
}
感觉这里的更加简便
特别是关于输入那里是很值得学习的
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//输入
int x = in.next().charAt(1) - '0';
//维护a,b
int a = 1189, b = 841;
for(int i = 1; i <= x; i++)
{
a /= 2;
if(a < b)
{
int temp = a;
a = b;
b = temp;
}
}
System.out.println(a);
System.out.println(b);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/f4fd666689ee6053a916ec888bab7590.png)
大道至拙