可憐的小娃娃啊。哥幫你寫了。先看效果圖(綠色為輸入):
承德網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),承德網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為承德成百上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的承德做網(wǎng)站的公司定做!
代碼如下:
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;
}
}
有疑問請(qǐng)追問或私信我,沒有用list和數(shù)組,望采納。
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("請(qǐng)輸入第"+i+"一個(gè)分?jǐn)?shù)(結(jié)束請(qǐng)輸入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)+"個(gè)分?jǐn)?shù)");
System.out.println("總成績(jī)是"+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]+"優(yōu)秀");
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;
}
}
}
我的代碼很簡(jiǎn)潔,采納我唄。。。
抽取問題, 重點(diǎn)是 同一個(gè)學(xué)號(hào)不能重復(fù)被抽取.
解決辦法很多,
比如數(shù)組可以使用下標(biāo)來(lái)標(biāo)記,號(hào)碼是否被使用,使用了就繼續(xù)下一次抽取
也可以使用集合來(lái)抽取,把集合順序打亂,然后隨便抽幾個(gè)就可以了
參考代碼:數(shù)組法
import?java.util.Random;
public?class?Test?{
public?static?void?main(String[]?args)?{
int?stuNums=30;
int[]?nums=new?int[stuNums];//存儲(chǔ)學(xué)號(hào)的數(shù)組
boolean[]?flags=new?boolean[stuNums];//標(biāo)記,用于標(biāo)記對(duì)應(yīng)下標(biāo)的學(xué)號(hào)是否已經(jīng)被抽取過了
for?(int?i?=?0;?i??stuNums;?i++)?{
nums[i]=i+1;//給學(xué)號(hào)賦值
}
Random?r=new?Random();
while(true){
int?index?=?r.nextInt(stuNums);
if(!flags[index]){
System.out.println("A等:"+nums[index]);
flags[index]=true;?//標(biāo)記已經(jīng)被使用過了
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--;//如果已經(jīng)被抽取過了?,那么i建議,再次循環(huán)
}
}
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));
}
}