重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
可怜的小娃娃啊。哥帮你写了。先看效果图(绿色为输入):
承德网站建设公司创新互联,承德网站设计制作,有大型网站制作公司丰富经验。已为承德成百上千提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的承德做网站的公司定做!
代码如下:
package zhidao;
import java.util.Scanner;
public class TestCount {
public static void main(String[] args) {
double total = 0;
int count = 0;
int max = -1;
int min = 11;
int countOthers = 0;
int count6 = 0;
int count7 = 0;
int count8 = 0;
int count9 = 0;
int count10 = 0;
Scanner scanner = new Scanner(System.in);
String line = null;
while(true){
System.out.println("Choose (A: add scores), (N: new scores), or (Q: quit)");
line = scanner.nextLine();
if(line.equalsIgnoreCase("Q")){
break;
}else if(line.equalsIgnoreCase("N")){
total = 0;
count = 0;
max = -1;
min = 11;
countOthers = 0;
count6 = 0;
count7 = 0;
count8 = 0;
count9 = 0;
count10 = 0;
System.out.println("Type the additional scores in single line");
line = scanner.nextLine();
}else if(line.equalsIgnoreCase("A")){
System.out.println("Type a new list of scores in single line");
line = scanner.nextLine();
}
String[] datas = line.split(" ");
for (String data : datas) {
if(!data.matches("^\\d+$")){
continue;
}
int d = Integer.parseInt(data);
if(d 0 || d 10){
continue;
}
total += d;
count++;
if(max d){
max = d;
}
if(min d){
min = d;
}
switch (d) {
case 10:
count10++;
break;
case 9:
count9++;
break;
case 8:
count8++;
break;
case 7:
count7++;
break;
case 6:
count6++;
break;
default:
countOthers++;
break;
}
}
System.out.println("\n------------------------");
System.out.println(" 6|" + nStart(countOthers));
System.out.println(" 6|" + nStart(count6));
System.out.println(" 7|" + nStart(count7));
System.out.println(" 8|" + nStart(count8));
System.out.println(" 9|" + nStart(count9));
System.out.println(" 10|" + nStart(count10));
System.out.printf("Average = %5.2f\n",total/count);
System.out.printf("Maximum = %5d\n",max);
System.out.printf("Minimum = %5d\n",min);
System.out.println("------------------------\n");
}
System.out.println("*** End of program ***");
}
private static String nStart(int count) {
String s = "";
for (int i = 0; i count; i++) {
s += "*";
}
return s;
}
}
有疑问请追问或私信我,没有用list和数组,望采纳。
import java.util.Scanner;
public class test {
static int s;
public static void main(String[] argv) {
int [] num=new int[20];
Scanner scanner=new Scanner(System.in);
Integer a;
int i;
for(i=1;i=20;i++){
System.out.println("请输入第"+i+"一个分数(结束请输入520嘿嘿):");
a=scanner.nextInt();
num[i-1]=a;
if(a==520){
break;
}
}
for(int m=0;mi-1;m++)
{
s=s+num[m];
}
System.out.println("总共录入了"+(i-1)+"个分数");
System.out.println("总成绩是"+s+"分");
System.out.println("平均分是"+s/(i-1)+"分");
for(Integer m=0;mnum.length-1;m++)
{ if(num[m]0num[m]!=520){
if(num[m]=90)
System.out.println(num[m]+"优秀");
else if(num[m]=80)
System.out.println(num[m]+"良好");
else if(num[m]=70)
System.out.println(num[m]+"中等");
else if(num[m]=60)
System.out.println(num[m]+"及格");
else
System.out.println(num[m]+"不及格");
}else break;
}
}
}
我的代码很简洁,采纳我呗。。。
抽取问题, 重点是 同一个学号不能重复被抽取.
解决办法很多,
比如数组可以使用下标来标记,号码是否被使用,使用了就继续下一次抽取
也可以使用集合来抽取,把集合顺序打乱,然后随便抽几个就可以了
参考代码:数组法
import java.util.Random;
public class Test {
public static void main(String[] args) {
int stuNums=30;
int[] nums=new int[stuNums];//存储学号的数组
boolean[] flags=new boolean[stuNums];//标记,用于标记对应下标的学号是否已经被抽取过了
for (int i = 0; i stuNums; i++) {
nums[i]=i+1;//给学号赋值
}
Random r=new Random();
while(true){
int index = r.nextInt(stuNums);
if(!flags[index]){
System.out.println("A等:"+nums[index]);
flags[index]=true; //标记已经被使用过了
break;
}
}
for (int i = 0; i 2; i++) {
int index = r.nextInt(stuNums);
if(!flags[index]){
System.out.println("B等:"+nums[index]);
flags[index]=true;
}else{
i--;//如果已经被抽取过了 ,那么i建议,再次循环
}
}
for (int i = 0; i 3; i++) {
int index = r.nextInt(stuNums);
if(!flags[index]){
System.out.println("c等:"+nums[index]);
flags[index]=true;
}else{
i--;
}
}
}
}
集合法
import java.util.ArrayList;
import java.util.Collections;
public class Test2 {
public static void main(String[] args) {
int stuNums=20;
ArrayListInteger list=new ArrayListInteger();
for (int i = 0; i stuNums; i++) {
list.add(i+1);
}
System.out.println("有序"+list);
Collections.shuffle(list);//打乱顺序
System.out.println("乱序"+list);
System.out.println("A等"+list.get(0));
System.out.println("B等"+list.get(1));
System.out.println("B等"+list.get(2));
System.out.println("C等"+list.get(3));
System.out.println("C等"+list.get(4));
System.out.println("C等"+list.get(5));
}
}